0

I have the following Django models:

class Product(models.Model):
    name = models.CharField(max_length=250, unique=True)
    quantity = models.IntegerField(default=0)

class Order(models.Model):
    products = models.ManyToManyField(
        Product,
        through='PositionInOrder'
    )
    discount = models.CharField(max_length=50, blank=True)
    total_sum = models.DecimalField(max_digits=11, decimal_places=2, default=0)

class PositionInOrder(models.Model):
    sale = models.ForeignKey(Order)
    product = models.ForeignKey(Product)
    quantity = models.PositiveIntegerField()
    price = models.PositiveIntegerField()

And the following serializers:

class ProductSerializer(serializers.ModelSerializer):

    class Meta:
        model = Product
        fields = '__all__'

class PositionInOrderSerializer(serializers.HyperlinkedModelSerializer):
    sale = serializers.ReadOnlyField()
    product = serializers.ReadOnlyField()

    class Meta:
        model = PositionInOrder
        fields = "__all__"

class OrderSerializer(serializers.ModelSerializer):
    products = PositionInOrderSerializer(many=True)

    def create(self, validated_data):
        products = validated_data.pop('products')
        order = Order.objects.create(**validated_data)
        return order

    class Meta:
        model = Order
        fields = '__all__'

I want to create new Order. Send post query which contains this json:

{
    "products": [{"id": 2606, "name": "Some name", "quantity": 140, "price": 250}],
    "discount": "15",
    "total_sum": 500
}

And in the create() method of a class OrderSerialization I obtain that products=: [OrderedDict([('quantity', 140), ('price', 250)])] and there are no information about product_id and product_name. How can I get them?

Radren
  • 307
  • 4
  • 6
  • Why you used `PositionInOrderSerializer` in `OrderSerializer` instead of `ProductSerializer`? – Ritesh Agrawal Feb 21 '18 at 17:43
  • My code based on this answer: https://stackoverflow.com/a/17263583/4786619 I use model with through-table, therefore I have to use PositionInOrderSerializer. – Radren Feb 22 '18 at 08:31

1 Answers1

2

Try to explicit define id and name fields in PositionInOrderSerializer:

class PositionInOrderSerializer(serializers.HyperlinkedModelSerializer):
    sale = serializers.ReadOnlyField()
    id = serializers.IntegerField(source='product.id')
    name = serializers.IntegerField(source='product.name')

    class Meta:
        model = PositionInOrder
        fields = "__all__"
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
  • 1
    Yes, now it works! Thank you! It shout be corrected little bit, should be: name = serializers.CharField(source='product.name') – Radren Feb 22 '18 at 08:39