Skip to content

Commit 79c8d26

Browse files
authored
Merge pull request TandoorRecipes#1311 from smilerz/patch-2
fix bug creating food with create form
2 parents 9486b08 + 0e1153c commit 79c8d26

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

cookbook/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ def get_or_create(self, *args, **kwargs):
7777
for field in many_to_many:
7878
field_model = getattr(obj, field).model
7979
for related_obj in many_to_many[field]:
80-
getattr(obj, field).add(field_model.objects.get(**dict(related_obj)))
80+
if isinstance(related_obj, User):
81+
getattr(obj, field).add(field_model.objects.get(id=related_obj.id))
82+
else:
83+
getattr(obj, field).add(field_model.objects.get(**dict(related_obj)))
8184
return obj, True
8285
except IntegrityError as e:
8386
if 'Key (path)' in e.args[0]:

cookbook/serializer.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,20 @@ def create(self, validated_data):
394394
validated_data['supermarket_category'], sc_created = SupermarketCategory.objects.get_or_create(
395395
name=validated_data.pop('supermarket_category')['name'],
396396
space=self.context['request'].space)
397-
onhand = validated_data.get('food_onhand', None)
397+
onhand = validated_data.pop('food_onhand', None)
398398

399399
# assuming if on hand for user also onhand for shopping_share users
400400
if not onhand is None:
401401
shared_users = [user := self.context['request'].user] + list(user.userpreference.shopping_share.all())
402+
if self.instance:
403+
onhand_users = self.instance.onhand_users.all()
404+
else:
405+
onhand_users = []
402406
if onhand:
403-
validated_data['onhand_users'] = list(self.instance.onhand_users.all()) + shared_users
407+
validated_data['onhand_users'] = list(onhand_users) + shared_users
404408
else:
405-
validated_data['onhand_users'] = list(set(self.instance.onhand_users.all()) - set(shared_users))
409+
validated_data['onhand_users'] = list(set(onhand_users) - set(shared_users))
410+
406411
obj, created = Food.objects.get_or_create(**validated_data)
407412
return obj
408413

vue/src/components/GenericHorizontalCard.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ export default {
238238
})
239239
popper.update()
240240
this.over = false
241-
this.$emit({ action: "drop", target: this.item, source: this.source })
242241
} else {
243242
this.isError = true
244243
}

0 commit comments

Comments
 (0)