def to_representation(self, instance): representation = super(CustomerSerializer, self).to_representation(instance) related_models = ['country', 'user'] for model in related_models: try: representation[model] = utils.to_dict(getattr(instance, model)) except: representation[model] = None try: representation['billingcontact'] = utils.to_dict( models.Contact.objects.instance_of( models.BillingContact).filter( id__in=instance.contact_set.values_list( 'id', flat=True)).first()) except: representation['billingcontact'] = None try: representation['shippingcontact'] = utils.to_dict( models.Contact.objects.instance_of( models.ShippingContact).filter( id__in=instance.contact_set.values_list( 'id', flat=True)).first()) except: representation['shippingcontact'] = None return representation
def to_dict(self): return { "user_id": self.user.pk, "username": self.user.username, "style": to_dict(self.style), "group": self.group.pk if self.group is not None else None }
def to_dict(self): return { "id": self.pk, "distribution": to_dict(self.distribution), "title": self.title, "author": self.author.pk, "content": self.content, "created_time": self.created, }
def to_representation(self, instance): representation = super(SupplierSerializer, self).to_representation(instance) related_models = ['country'] for model in related_models: try: representation[model] = utils.to_dict(getattr(instance, model)) # print(representation[model]) except: representation[model] = None return representation
def to_representation(self, instance): representation = super(InventorySerializer, self).to_representation(instance) related_models = [ 'product_category', 'supplier', 'product_manufacturer' ] for model in related_models: try: representation[model] = utils.to_dict(getattr(instance, model)) except: representation[model] = None return representation
def to_representation(self, instance): representation = super(EnquirySerializer, self).to_representation(instance) related_models = [ 'country', ] for model in related_models: try: representation[model] = utils.to_dict(getattr(instance, model)) except: representation[model] = None # # try: # representation['part_number'] = InventorySerializer(instance.part_number, many=True).data # except: # representation['part_number'] = None try: product_enquiry = [] productenquires = inventory_model.ProductEnquiry.objects.filter( enquiry=instance) for p_e in productenquires: p_e = ProductEnquirySerializer(p_e).data p_e.pop('enquiry') product_enquiry.append(p_e) representation['product_enquiry'] = product_enquiry except: representation['product_enquiry'] = None try: representation['company'] = utils.to_dict(instance.company) # representation['company'] = ContactSerializer(instance.company_name).data except: representation['company'] = None return representation
def test_get_coders_by_style(self): user1 = User.objects.create_user(username="******", password="******", email="*****@*****.**", salt="123", role=1) user2 = User.objects.create_user(username="******", password="******", email="*****@*****.**", salt="123", role=1) user3 = User.objects.create_user(username="******", password="******", email="*****@*****.**", salt="123", role=1) coding_style1 = CodingStyle(style=CodingStyle.Style.UITC, UM_value=0.0, TI_value=0.0, RT_value=0.0, JC_value=0.0) coding_style1.save() coding_style2 = CodingStyle(style=CodingStyle.Style.UITC, UM_value=0.0, TI_value=0.0, RT_value=0.0, JC_value=0.0) coding_style2.save() coding_style3 = CodingStyle(style=CodingStyle.Style.UITC, UM_value=0.0, TI_value=0.0, RT_value=0.0, JC_value=0.0) coding_style3.save() coder1 = Coder(user=user1, style=coding_style1) coder1.save() coder2 = Coder(user=user2, style=coding_style2) coder2.save() coder3 = Coder(user=user3, style=coding_style3) coder3.save() expected_response = [{ "user_id": user1.pk, "username": "******", "style": to_dict(coding_style1), "group": None }, { "user_id": user2.pk, "username": "******", "style": to_dict(coding_style2), "group": None }, { "user_id": user3.pk, "username": "******", "style": to_dict(coding_style3), "group": None }] client = Client() res = client.get("/api/analysis/style/8/") self.assertEqual(res.content.decode(), json.dumps(expected_response))