Search code examples
djangodjango-modelsdjango-rest-frameworkmany-to-manydjango-orm

DRF: creating new object in a model with 'ManyToMany' field and 'through' table


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?


Solution

  • 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__"