我有 1 或 multiple 的发票product_invoice
,有些product_invoice
可以有 1 product_order
(所以在 ProductOrder 中,productinvoiceid 应该是 OneToOne relashionship,但这对我的问题并不重要)。
我能够获取 2 级的数据,但无法创建,出现此错误:
TypeError:'orderinfo' 是此函数的无效关键字参数。
但如果我删除orderinfo
,ProductInvoiceSerializer
我可以创建 Invoice 和相关Product_Invoice
的 .
我做错了什么?
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
}
]
}
]
},
我的模型:
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'
我的序列化器:
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)
for 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)
for product_data in products_data:
#product_data.invoiceid = invoice.invoiceid
ProductInvoice.objects.create(invoiceid=invoice, **product_data)
return invoice
更新 2016-05-11
视图.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)