From 8d568526be1bc2b95b287daaedf75fb136716946 Mon Sep 17 00:00:00 2001 From: Joshua Bonnett Date: Wed, 29 Jun 2022 05:15:22 -0700 Subject: [PATCH] Fixes #1857 --- cookbook/models.py | 3 ++- cookbook/tests/api/test_api_food.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/cookbook/models.py b/cookbook/models.py index 8bd2e22c5..89795be02 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -610,8 +610,9 @@ class Food(ExportModelOperationsMixin('food'), TreeModel, PermissionModelMixin): tree_filter = Q(space=space) # remove all inherited fields from food - trough = Food.objects.filter(tree_filter).first().inherit_fields.through + trough = Food.inherit_fields.through trough.objects.all().delete() + # food is going to inherit attributes if len(inherit) > 0: # ManyToMany cannot be updated through an UPDATE operation diff --git a/cookbook/tests/api/test_api_food.py b/cookbook/tests/api/test_api_food.py index 9049a7906..98376280f 100644 --- a/cookbook/tests/api/test_api_food.py +++ b/cookbook/tests/api/test_api_food.py @@ -549,6 +549,19 @@ def test_reset_inherit_space_fields(obj_tree_1, space_1, global_reset, field): assert getattr(obj_tree_1, field) == getattr(child, field) +@pytest.mark.parametrize("obj_tree_1", [ + ({'has_category': True, 'inherit': False, 'ignore_shopping': True, 'substitute_children': True, 'substitute_siblings': True}), +], indirect=['obj_tree_1']) +@pytest.mark.parametrize("field", ['ignore_shopping', 'substitute_children', 'substitute_siblings', 'supermarket_category']) +def test_reset_inherit_no_food_instances(obj_tree_1, space_1, field): + with scope(space=space_1): + parent = obj_tree_1.get_parent() + Food.objects.all().delete() + + space_1.food_inherit.add(*Food.inheritable_fields.values_list('id', flat=True)) # set default inherit fields + parent.reset_inheritance(space=space_1) + + def test_onhand(obj_1, u1_s1, u2_s1): assert json.loads(u1_s1.get(reverse(DETAIL_URL, args={obj_1.id})).content)['food_onhand'] == False assert json.loads(u2_s1.get(reverse(DETAIL_URL, args={obj_1.id})).content)['food_onhand'] == False