The task is to create a JSON file with a complete list of orders in the online store. Expected Result:

[ { "delivery": { "delivery_time": "2016-07-06T14:14:26Z", "delivery_adress": "Тестовый адрес тоставки", "phone": "12345678" }, 'products': [ {"product": 1,"count": "2"}, {"product": 2,"count": "1"}, {"product": 3,"count": "4"}, ... ], "paymentMethod": { "id": 2 }, "summ": "1900", "success": false } ] 

There are several serializers for this purpose, at the moment the delivery and paymentMethod serializer is working correctly, it’s impossible to add products.

 class OrderProductSerializer(serializers.ModelSerializer): class Meta: model = OrderProduct fields = ('product','count') def create(self, validated_data): return OrderProduct.objects.create(**validated_data) class OrderSerializer(serializers.ModelSerializer): paymentMethod = PaymentsSerializer(required=False, allow_null=True) delivery = DeliverySerializer(required=False, allow_null=True) orderProduct_set=OrderProductSerializer(many=True) class Meta: model = Order fields =('delivery','paymentMethod','summ','success','orderProduct_set'), 

The error that I get

 Got AttributeError when attempting to get a value for field `orderProduct_set` on serializer `OrderSerializer`. The serializer field might be named incorrectly and not match any attribute or key on the `Order` instance. Original exception text was: 'Order' object has no attribute 'orderProduct_set'. 

Models

 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 __int__(self): return 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.CharField(max_length=15,default='0') product = models.ForeignKey('product.Product') count = models.CharField(max_length=15,default='0') def __int__(self): return self.id 

Closed due to the fact that off-topic participants aleksandr barakin , fori1ton , user194374, D-side , zRrr Jul 11 ​​'16 at 21:15 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - aleksandr barakin, fori1ton, community spirit, D-side, zRrr
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    orderproduct_set probably in lower case, register matters - FeroxTL
  • Yes, that's right, the register change was done - Weit

1 answer 1

On the advice of @feroxtl, the register shift in

 orderProduct_set=OrderProductSerializer(many=True) class Meta: model = Order fields =('delivery','paymentMethod','summ','success','orderProduct_set'), 

Correct option

 orderproduct_set=OrderProductSerializer(many=True) class Meta: model = Order fields =('delivery','paymentMethod','summ','success','orderproduct_set'),