json - How to serialize relation -
try post json api serializer:
from rest_framework import serializers models import order, orderproduct, delivery, payments class deliveryserializer(serializers.modelserializer): class meta: model = delivery fields = ('delivery_time','delivery_adress','phone')#'id', def create(self, validated_data): """ create , return new `snippet` instance, given validated data. """ return delivery.objects.create(**validated_data) class paymentsserializer(serializers.modelserializer): class meta: model = payments #fields = (title) #'id', def create(self, validated_data): """ create , return new `snippet` instance, given validated data. """ return payments.objects.create(**validated_data) class orderserializer(serializers.modelserializer): delivery = deliveryserializer(required=false, allow_null=true) paymentmethod = paymentsserializer(required=false, allow_null=true) class meta: model = order fields = ( 'delivery', 'paymentmethod','summ','success') #'id', def create(self, validated_data): deliverys_data = validated_data.pop('delivery') paymentsmethod_data = validated_data.pop('paymentmethod') order = order.objects.create(**validated_data) delivery.objects.create(order=order,**deliverys_data) payments.objects.create(order=order,**paymentsmethod_data) return order
view:
api_view(['post']) def order_post(request, format=none) if request.method == 'post': serializer = orderserializer(data=request.data) if serializer.is_valid(): serializer.save() return response(serializer.data, status=status.http_201_created) return response(serializer.errors, status=status.http_400_bad_request)
when 1 try put order in database :
return database.cursor.execute(self, query, params) integrityerror: order_order.delivery_id may not null
i try remove id fields, add id fields, result same.
json:
{"delivery":{"delivery_time":"2016-05-31t12:18:47z","delivery_adress":"123","phone":"123"},"paymentmethod":{"id":1,"title":"123124123"},"summ":"23","success":false}
or
{"delivery":{"id":"1","delivery_time":"2016-05-31t12:18:47z","delivery_adress":"123","phone":"123"},"paymentmethod":{"id":"1","title":"123124123"},"summ":"23","success":false}
upd1. models:
from __future__ import unicode_literals django.db import models # create models here. class order (models.model): id = models.autofield(primary_key=true) date_create = models.datefield(auto_now_add=true) date_change = models.datefield(auto_now=true) summ =models.charfield(max_length=15,default='0') delivery = models.foreignkey('delivery') success = models.booleanfield(default=false) paymentmethod = models.foreignkey('payments') def __unicode__(self): return unicode(self.id) class orderproduct(models.model): order=models.foreignkey('order') id = models.autofield(primary_key=true) date_create = models.datefield(auto_now_add=true) date_change = models.datefield(auto_now=true) price = models.integerfield() product = models.foreignkey('product.product') additionals = models.integerfield(null=true,default=0) count = models.integerfield() def __unicode__(self): return self.id class delivery(models.model): id = models.autofield(primary_key=true) date_create = models.datefield(auto_now_add=true) date_change = models.datefield(auto_now=true) delivery_time = models.datetimefield() delivery_adress = models.textfield() phone = models.textfield() def __unicode__(self): return self.phone class payments(models.model): id = models.autofield(primary_key=true) date_create = models.datefield(auto_now_add=true) date_change = models.datefield(auto_now=true) title = models.textfield(); def __unicode__(self): return self.title
you doing object creations in wrong order, that's why integrityerror: order_order.delivery_id may not null
. check model structures again:
your order
models has foreignkey's
delivery
, payments
, means need create delivery
, payments
objects before order
.
try this:
def create(self, validated_data): deliverys_data = validated_data.pop('delivery') delivery = delivery.objects.create(**deliverys_data) paymentsmethod_data = validated_data.pop('paymentmethod') payment_method = payments.objects.create(**paymentsmethod_data) order = order.objects.create( delivery=delivery, paymentmethod=payment_method, **validated_data ) return order
Comments
Post a Comment