-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Expand file tree
/
Copy pathserializers.py
More file actions
30 lines (22 loc) · 843 Bytes
/
serializers.py
File metadata and controls
30 lines (22 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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(source='itemamount_set', many=True)
def create(self, validated_data):
items = validated_data.pop('itemamount_set')
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', )