Answer the question
In order to leave comments, you need to log in
Why does an AttributeError occur?
Mistake:
AttributeError at /orders/
'NoneType' object has no attribute 'add'
def create(self, validated_data):
address_data = dict(validated_data.pop('address'))
print address_data
address = Address.objects.create(**address_data)
instance = Order.objects.create(address=address, **validated_data)
session_id = self.context['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.add(burger)
return instance
Answer the question
In order to leave comments, you need to log in
In addition to changing the burgers field type from ForeignKey to ManyToManyField, it's better to change the code like this
burger_ids = [i.burger_id for i in SessionOrder.objects.filter(session_id=session_id)]
burgers = Burger.objects.filter(id__in=burger_ids)
instance.burgers.add(*burgers)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question