serialization - Writable Double-nested serializers with Django Rest Framework -


i have invoice 1 or multiple product_invoice, , product_invoice can have 1 product_order (so in productorder, productinvoiceid shoulb onetoone relashionship not important issue).

i able data on 2 level, can't create, there error : typeerror: 'orderinfo' invalid keyword argument function.

but if remove 'orderinfo' in productinvoiceserializer able create invoice , related product_invoice.

what doing wrong ?

django : 1.9.3, drf : 3.3.3

[ {     "invoiceid": 43,     "stocklocationid": 1,     "invoicecode": "obr00040",     "invoicedate": "2016-05-07",     "totalamount": 500000,     "buypricetotal": 125000,     "discount": 0,     "ppnamount": 50000,     "ispaid": true,     "deposit": 0,     "emailaddress": "",     "customername": "",     "invoicenote": "",     "isorder": false,     "deliverydate": null,     "products": [         {             "productinvoiceid": 48,             "productid": 1,             "quantity": 1,             "buyingprice": 200000,             "saleprice": 100000,             "orderinfo": [                 {                     "productorderid": 2,                     "note": "",                     "isstock": false,                     "stocklocationid": 1,                     "ispickedup": 0                 }             ]         }     ] }, 

my model :

class invoice(models.model):     invoiceid = models.autofield(db_column='invoiceid', primary_key=true)     invoicecode = models.charfield(db_column='invoicecode', unique=true, max_length=8)      class meta:         managed = false         db_table = 'invoice'  class productinvoice(models.model):     productinvoiceid = models.autofield(db_column='productinvoiceid', primary_key=true)     productid = models.foreignkey(product, models.do_nothing, db_column='productid')     invoiceid = models.foreignkey(invoice, models.do_nothing, db_column='invoiceid', related_name='products')     quantity = models.integerfield(db_column='quantity', verbose_name='quantity')  class meta:     managed = false     db_table = 'product_invoice'  class productorder(models.model):     productorderid = models.autofield(db_column='productorderid', primary_key=true)     productinvoiceid = models.foreignkey(productinvoice, models.do_nothing, db_column='productinvoiceid', related_name='orderinfo')     isstock = models.booleanfield(db_column='stock', verbose_name = 'is stock ?')     isreadytopick = models.integerfield(db_column='readytopick')     ispickedup = models.integerfield(db_column='pickedup', verbose_name = 'already picked-up ?')  class meta:     managed = false     db_table = 'product_order' 

my serializer :

class productorderserializer(serializers.modelserializer):     class meta:         model = productorder         fields = ('productorderid','note','isstock','stocklocationid','ispickedup')  class productinvoiceserializer(serializers.modelserializer):     orderinfo = productorderserializer(many=true)      class meta:         model = productinvoice         fields = ('productinvoiceid', 'productid', 'quantity', 'buyingprice', 'saleprice', 'orderinfo')     #fields = ('productinvoiceid', 'productid', 'quantity', 'buyingprice', 'saleprice')      def create(self, validated_data):         ordersinfo_data = validated_data.pop('orderinfo')         product_invoice = productinvoice.objects.create(**validated_data)         orderinfo_data in ordersinfo_data:             productorder.objects.create(productinvoiceid=product_invoice, **orderinfo_data)         return product_invoice  class invoiceserializer(serializers.modelserializer):     products = productinvoiceserializer(many=true)      class meta:         model = invoice         fields = ('invoiceid', 'stocklocationid', 'invoicecode','invoicedate','totalamount','buypricetotal','discount','ppnamount','ispaid','deposit','emailaddress','customername','invoicenote','isorder','deliverydate','products')      def create(self, validated_data):         products_data = validated_data.pop('products')         invoice = invoice.objects.create(**validated_data)         product_data in products_data:             #product_data.invoiceid = invoice.invoiceid             productinvoice.objects.create(invoiceid=invoice, **product_data)         return invoice 

update 2016-05-11

view.py

@api_view(['get', 'post']) def invoice_list(request):  if request.method == 'get':     invoices = invoice.objects.all()     serializer = invoiceserializer(invoices, many=true)     return response(serializer.data)  elif request.method == 'post':     serializer = invoiceserializer(data=request.data)     if serializer.is_valid():         serializer.save()         return jsonresponse(serializer.data, status=status.http_201_created, )     return jsonresponse(serializer.errors, status=status.http_400_bad_request) 

since manually creating productinvoice object here @ productinvoice.objects.create(invoiceid=invoice, **product_data) , productinvoice model doesn't have field named orderinfo.

now since using

orderinfo = productorderserializer(many=true) 

productinvoice needs have manytomany field store orderinfo

thus model should this:

class productinvoice(models.model):     productinvoiceid = models.autofield(db_column='productinvoiceid', primary_key=true)     productid = models.foreignkey(product, models.do_nothing, db_column='productid')     invoiceid = models.foreignkey(invoice, models.do_nothing, db_column='invoiceid', related_name='products')     quantity = models.integerfield(db_column='quantity', verbose_name='quantity')     orderinfo = models.manytomanyfield(productorder) 

or

you should use productinvoiceserializer create productinvoice object:

def create(self, validated_data):         products_data = validated_data.pop('products')         invoice = invoice.objects.create(**validated_data)         product_data in products_data:             #product_data.invoiceid = invoice.invoiceid             data = product_data             data['invoiceid'] = invoice             serializer = productinvoiceserializer(data=data)             if serializer.is_valid(raise_exception=true):                 serializer.save()         return invoice 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -