Skip to content

Conversation

@Mohit-Lakra
Copy link

@Mohit-Lakra Mohit-Lakra commented Oct 27, 2025

Add reordering for Countries and Ingredients; fix ingredients data

Summary

This PR implements reordering support for Country and BreadIngredient models, enabling drag-and-drop reordering in the Wagtail admin interface (Wagtail 7.2+). It also fixes the broken ingredients data issue (#564).

Closes #565. Fixes #564.

Changes

Models (Country & BreadIngredient)

  • Added sort_order field to both models
    • Country: explicit IntegerField with db_index=True
    • BreadIngredient: uses Orderable mixin from Wagtail
  • Updated Meta.ordering to prioritize sort_order, then fall back to title/name

Admin Interface

  • Added sort_order_field = "sort_order" to CountryModelViewSet and BreadIngredientSnippetViewSet
  • Updated default ordering in viewsets to use sort_order first

Data Migrations

  • Migration 0010: Schema changes to add sort_order fields and update ordering
  • Migration 0011: Data migration to backfill sort_order for existing records
    • Assigns values based on alphabetical order
    • Uses increments of 10 for flexibility
    • Includes reversible migration

Demo Data

  • Updated fixtures to include sort_order values for all Countries and Ingredients
  • Values assigned alphabetically for predictable initial ordering

Templates

  • Updated bread_page.html to respect sort_order when displaying ingredients
  • Changed from .all() to .order_by('sort_order', 'name')

Tests

  • Added comprehensive tests for both models
  • Tests verify sort_order field existence and behavior
  • Tests verify ordering respects sort_order values
  • Tests verify NULL handling and fallback ordering

Behavior

Wagtail 7.1 (Current)

  • Models have sort_order fields and respect ordering
  • Admin interface works normally
  • No "Sort item order" button visible (feature not available in 7.1)
  • Backward compatible - no breaking changes

Wagtail 7.2+ (Future)

  • "Sort item order" button appears in admin list views
  • Drag-and-drop reordering persists changes to sort_order
  • Manual ordering survives page reloads and migrations

QA Checklist

  • Migrations run cleanly on fresh database
  • Demo data loads with correct sort_order values
  • Model ordering includes sort_order in Meta
  • Admin viewsets configured with sort_order_field
  • Template respects ordering when displaying ingredients
  • Tests pass for both models
  • Pre-commit hooks pass (black, ruff, prettier)
  • Backward compatible with Wagtail 7.1
  • Manual test: drag-drop reordering in admin (requires Wagtail 7.2+)
  • Manual test: ingredient ordering on bread detail pages

Testing Instructions

Fresh Install

# Setup
python -m venv venv
source venv/bin/activate
pip install -r requirements/base.txt
python manage.py migrate
python manage.py load_initial_data

# Verify sort_order values
python manage.py shell -c "
from bakerydemo.breads.models import Country, BreadIngredient
print('Countries:', list(Country.objects.values_list('sort_order', 'title')[:5]))
print('Ingredients:', list(BreadIngredient.objects.values_list('sort_order', 'name')))
"

# Run tests
python manage.py test bakerydemo.breads.tests

Upgrade from Existing Database

# Backup your database first!
python manage.py migrate breads

# Verify backfill worked
python manage.py shell -c "
from bakerydemo.breads.models import Country, BreadIngredient
print('Countries with sort_order:', Country.objects.filter(sort_order__isnull=False).count())
print('Ingredients with sort_order:', BreadIngredient.objects.filter(sort_order__isnull=False).count())
"

Manual Admin Testing (Wagtail 7.2+)

  1. Navigate to admin → Bread Categories → Countries
  2. Click "Sort item order" button (should be visible in 7.2+)
  3. Drag items to reorder
  4. Save changes
  5. Verify order persists on page reload
  6. Repeat for Bread Ingredients

Implementation Notes

Why Orderable for BreadIngredient but not Country?

  • BreadIngredient already uses DraftStateMixin and RevisionMixin, so adding Orderable is consistent with the pattern
  • Country is a simpler model without draft/revision support, so a plain IntegerField is more appropriate
  • Both approaches work identically with Wagtail's reordering UI

Why Increments of 10?

Using increments of 10 (10, 20, 30...) instead of 1, 2, 3 provides flexibility:

  • Easy to insert items between existing ones (e.g., between 10 and 20, add 15)
  • Reduces need for bulk reordering operations
  • Common pattern in sortable systems

Fixture Update Strategy

The fixture was updated using a Python script that:

  1. Loaded the existing JSON fixture
  2. Sorted Countries/Ingredients alphabetically
  3. Assigned sort_order values in increments of 10
  4. Preserved all other fields and relationships

Commits

  1. feat(breads): add sort_order field and enable admin reordering - Core functionality
  2. chore(breads): add data migration to backfill sort_order values - Data migration
  3. fix(breads): populate sort_order in fixtures and respect ordering in templates - Demo data and UI
  4. test(breads): add ordering and sort_order field tests - Test coverage

Related Issues

image image

…untry and BreadIngredient (wagtail#565)

- Add sort_order field to Country model with db_index
- Add Orderable mixin to BreadIngredient model
- Update model Meta ordering to prioritize sort_order
- Add sort_order_field to CountryModelViewSet and BreadIngredientSnippetViewSet
- Enable drag-and-drop reordering in Wagtail admin (7.2+)
- Maintain backward compatibility with Wagtail 7.1
…ail#565)

- Backfill sort_order for existing Country and BreadIngredient instances
- Assign values based on alphabetical order (name/title)
- Use increments of 10 for flexibility in future reordering
- Include reverse migration to restore None values
…templates (wagtail#564 wagtail#565)

- Add deterministic sort_order values to Country and BreadIngredient fixtures
- Sort fixtures alphabetically to ensure predictable display order
- Update bread_page.html template to order ingredients by sort_order
- Fixes missing ingredient data display issue (wagtail#564)
…error (wagtail#565)

- Add ordered_ingredients property to BreadPage model
- Return ingredients ordered by sort_order, then name
- Update template to use property instead of method call
- Fixes TemplateSyntaxError from .order_by() in template
@Mohit-Lakra Mohit-Lakra force-pushed the feature/reorder-ingredients-countries-565 branch from 1f4f482 to 067a4f2 Compare October 27, 2025 09:02
@Mohit-Lakra
Copy link
Author

Fixed template syntax error by adding ordered_ingredients property to BreadPage model. Django templates cannot call methods with arguments like .order_by(), so I created a property that returns the ordered queryset.

@thibaudcolas
Copy link
Member

Thank you for giving this a go @Mohit-Lakra. Note #564 and #565 were already assigned to someone else. In the future, if you see an issue has an assignee, it’s worth checking with them in the issue comments so multiple people aren’t needlessly working on the same task.

I see you’ve used ChatGPT for this, could you share more information about how you used it? Screenshots / copies of the prompts, or use the "Share" feature if you can so I can see the sequence? There are a lot of changes in here, I’d like to check its reasoning for some of them, and whether ChatGPT tested with Wagtail 7.2 or no.

@thibaudcolas thibaudcolas self-requested a review October 29, 2025 10:12
@Mohit-Lakra
Copy link
Author

Hi @thibaudcolas , sorry about that — I missed that the issue was already assigned to someone else. I’ll make sure to check for assignees and confirm in the comments before picking up any future issues.
Regarding ChatGPT — I don’t have the exact conversation history anymore since I was using it in terminal (CLI mode), and once the session was closed, the history wasn’t saved. I mainly used it to get some code suggestions and explanations

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.

Demo(s) of reordering support for models and snippets Broken ingredients data

2 participants