Skip to content

feat(nutrition): Refactor Ingredient categories to ManyToMany relationship (issue #984)#2414

Open
pankaj-basnet wants to merge 6 commits into
wger-project:masterfrom
pankaj-basnet:feat/nutrition--ingredient-categories-984
Open

feat(nutrition): Refactor Ingredient categories to ManyToMany relationship (issue #984)#2414
pankaj-basnet wants to merge 6 commits into
wger-project:masterfrom
pankaj-basnet:feat/nutrition--ingredient-categories-984

Conversation

@pankaj-basnet

@pankaj-basnet pankaj-basnet commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

PR Description:

This PR refactors the Ingredient model to support multiple categories per ingredient, moving away from a restricted 1:N relationship. This aligns with Open Food Facts data structures.

# File Path Change Summary
1 wger/nutrition/api/serializers.py Registered IngredientCategorySerializer and added to IngredientInfoSerializer.
2 wger/nutrition/api/views.py Added category to prefetch_related to optimize database queries.
3 wger/nutrition/dataclasses.py Added categories to data cleaning lifecycle.
4 wger/nutrition/extract_info/off.py Implemented parsing logic for nested category tags.
5 wger/nutrition/migrations/0038... Created data migration for safe transition from FK to M2M.
6 wger/nutrition/models/ingredient.py Added _assign_categories helper and atomic saving logic.

Related Issue(s)

Please check that the PR fulfills these requirements

  • Code has been formatted to avoid unnecessary diffs (ruff format && isort .)
  • If the feature is big enough or if there are manual steps needed (deployment
    changes etc.), write a small writeup in CHANGELOG.md

EDIT:

  • Tests for the changes have been added (for bug fixes / features)

…ns for OFF product categories

 This provides an intermediate structure
to hold multi-tiered category taxonomy lists retrieved from Open Food
Facts payloads before database persistence processing occurs.
…n script

Updated extract_info_from_off to parse the raw 'categories_tags' array
returned by the API. Refactored fetch_ingredient_from_off to iterate through
extracted values, ensuring related category entities are dynamically resolved
using get_or_create logic.
- Add data migration (0038) to preserve existing category associations.
- Add _assign_categories helper to Ingredient model.
- Add IngredientCategorySerializer to support nested category data.
- Optimize querysets using prefetch_related
@pankaj-basnet

pankaj-basnet commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

it seems 1 other test failed (not the tests for this PR)!

File "D:\src\PY\src\wger\wger\nutrition\tests\test_nutrition_plan_info.py", line 47, in test_query_count_independent_of_item_count
self.assertEqual(count_few, count_many)
AssertionError: 15 != 23


Ran 350 tests in 50.865s

FAILED (failures=1)

EDIT:
my bad!
i have to sharpen my "tests" coding skills in django , i guess ! all tests passed! 🎉

@pankaj-basnet
pankaj-basnet force-pushed the feat/nutrition--ingredient-categories-984 branch 3 times, most recently from 2062440 to 78ec8f6 Compare July 6, 2026 14:59
…t categories lists are parsed, cleaned, and assigned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add categories to ingredients

1 participant