Модель:
class Burger(models.Model):
name = models.CharField(max_length=64)
description = models.TextField(max_length=256)
price = models.DecimalField(max_digits=3,decimal_places=0)
class Order(models.Model):
count = models.IntegerField(default=0)
address = models.OneToOneField(Address,default='')
phone_number = models.IntegerField(default=0,blank=False)
burgers = models.ForeignKey(Burger, null=True)
Сериалайзер:
class BurgerSerializer(serializers.ModelSerializer):
class Meta:
model = Burger
fields = ['name', 'description', 'price']
class OrderSerializer(serializers.ModelSerializer):
address = AddressSerializer()
burgers = BurgerSerializer(read_only=True)
class Meta:
model = Order
fields = ['count', 'phone_number', 'address', 'burgers']
Вьюха:
class OrderList(generics.ListCreateAPIView):
# GET request to list all entrys
queryset = Order.objects.all()
serializer_class = OrderSerializer
def perform_create(self, serializer):
instance = serializer.save()
session_id = self.request.session.session_key
burger_ids = SessionOrder.objects.filter(session_id=session_id)
for item in burger_ids:
burger = Burger.objects.get(id=item.burger_id)
instance.burgers_set.add(burger)
В чем может быть причина ?