0

I want to design solution for ordering items. I have endpoint that create orders BUT I need to to have items object in the order. let me show you the code

class ItemModel(models.Model):
    name = models.CharField(max_length=50)
    price = models.FloatField()
    discretion = models.CharField(max_length=500)
    available = models.BooleanField(default=True)

class OrderModel(models.Model):
    phone = models.CharField(max_length=20)
    delevary_time = models.DateTimeField()

class CartModel(models.Model):
    order = models.ForeignKey(OrderModel, on_delete=models.CASCADE, related_name='order_m')
    item = models.ForeignKey(ItemModel, on_delete=models.CASCADE, related_name='item_m')

I need endpoint that create order to me. her what I did

class CartSerializer(serializers.ModelSerializer):
    class Meta:
        model = CartModel
        exclude = ['order',]
        depth = 2

class OrderSerializer(serializers.ModelSerializer):
    cart = serializers.SerializerMethodField()
    class Meta:
        model = OrderModel
        fields = ['phone', 'state', 'delevary_time', 'cart']

    def get_cart(self, obj):
        cart = CartModel.objects.filter(order__id=obj.id)
        serializer = CartSerializer(cart, many=True)
        return serializer.data

this is the endpoint

      router.register('order', OrderViewSet, 'api-order')
{
    "phone": 124997988698,
    "delevary_time": "2020-07-17T19:34:00",
    "cart": [
        {
            "item": 1
        },
        {
            "item": 2
        }
    ]
}

when I post the json it don't save the cart it only save the oder phone and delevary_time. How I can save the cart at the same time

6
  • It's to better use nested serializer for foreignkeys, but not SerializerMethodField, since it is readonly Commented Jul 18, 2020 at 8:33
  • how to do it ? can you give me clue Commented Jul 18, 2020 at 9:15
  • 1
    full example here -> django-rest-framework.org/api-guide/relations/… Commented Jul 18, 2020 at 9:41
  • it give me this error python Original exception text was: 'OrderModel' object has no attribute 'cart'. Commented Jul 18, 2020 at 10:14
  • i found that it save the model but the problem in Get request. I'don't know way !!.. Also, in Post request it save the model and show error message Commented Jul 18, 2020 at 10:26

1 Answer 1

1
class CartSerializer(serializers.ModelSerializer):
    class Meta:
        model = CartModel
        exclude = ['order',]
        depth = 2

class OrderSerializer(serializers.ModelSerializer):
    order_m = CartSerializer(many=True) # adding this
    class Meta:
        model = OrderModel
        fields = ['phone', 'state', 'delevary_time', 'order_m']
    
     def create(self, validated_data):
        cart_data = validated_data.pop('order_m')
        order = OrderModel.objects.create(**validated_data)
        for c in cart_data:
            CartModel.objects.create(order=order, **c)
        return order


Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.