mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-12-01 05:54:01 +03:00
31 lines
809 B
Python
31 lines
809 B
Python
|
from rest_framework import serializers
|
||
|
|
||
|
from . import models
|
||
|
|
||
|
|
||
|
class ItemAmountSerializer(serializers.ModelSerializer):
|
||
|
item = serializers.PrimaryKeyRelatedField(queryset=models.Item.objects.all())
|
||
|
|
||
|
class Meta:
|
||
|
model = models.ItemAmount
|
||
|
fields = ('item', 'amount')
|
||
|
|
||
|
|
||
|
class SummarySerializer(serializers.ModelSerializer):
|
||
|
items = ItemAmountSerializer(many=True)
|
||
|
|
||
|
def create(self, validated_data):
|
||
|
items = validated_data.pop('items')
|
||
|
instance = super().create(validated_data)
|
||
|
for item in items:
|
||
|
instance.items.add(
|
||
|
item['item'], through_defaults=dict(
|
||
|
amount=item['amount']
|
||
|
)
|
||
|
)
|
||
|
return instance
|
||
|
|
||
|
class Meta:
|
||
|
model = models.Summary
|
||
|
fields = ('items', )
|