mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 09:11:08 -05:00
2.2 KiB
2.2 KiB
Park Count Fields Implementation
Context
While implementing park views, we encountered errors where ride_count and coaster_count annotations conflicted with existing model fields of the same names. Additionally, we discovered inconsistencies in how these counts were being used across different views.
Decision
We decided to use both approaches but with distinct names:
-
Model Fields:
ride_count: Stored count of all ridescoaster_count: Stored count of roller coasters- Used in models and database schema
- Required for backward compatibility
-
Annotations:
current_ride_count: Real-time count of all ridescurrent_coaster_count: Real-time count of roller coasters- Provide accurate, up-to-date counts
- Used in templates and filters
This approach allows us to:
- Maintain existing database schema
- Show accurate, real-time counts in the UI
- Avoid name conflicts between fields and annotations
- Keep consistent naming pattern for both types of counts
Implementation
-
Views:
- Added base queryset method with annotations
- Used 'current_' prefix for annotated counts
- Ensured all views use the base queryset
-
Filters:
- Updated filter fields to use annotated counts
- Configured filter class to always use base queryset
- Maintained filter functionality with new field names
-
Templates:
- Updated templates to use computed counts
Why This Pattern
- Consistency: Using the 'current_' prefix clearly indicates which values are computed in real-time
- Compatibility: Maintains support for existing code that relies on the stored fields
- Flexibility: Allows gradual migration from stored to computed counts if desired
- Performance Option: Keeps the option to use stored counts for expensive queries
Future Considerations
We might want to:
- Add periodic tasks to sync stored counts with computed values
- Consider deprecating stored fields if they're not needed for performance
- Add validation to ensure stored counts stay in sync with reality
- Create a management command to update stored counts
Related Files
- parks/models.py
- parks/views.py
- parks/filters.py
- parks/templates/parks/partials/park_list_item.html
- parks/tests/test_filters.py