mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 11:51:10 -05:00
1.5 KiB
1.5 KiB
Ride Count Field Implementation
Context
While implementing park views, we encountered an error where a ride_count annotation conflicted with an existing model field of the same name. This raised a question about how to handle real-time ride counts versus stored counts.
Decision
We decided to use both approaches but with distinct names:
-
Model Field (
ride_count):- Kept the original field for backward compatibility
- Used in test fixtures and filtering system
- Can serve as a cached/denormalized value
-
Annotation (
current_ride_count):- Added new annotation with a distinct name
- Provides real-time count of rides
- Used in templates for display purposes
This approach allows us to:
- Maintain existing functionality in tests and filters
- Show accurate, real-time counts in the UI
- Avoid name conflicts between fields and annotations
Implementation
- Kept the
ride_countIntegerField in the Park model - Added
current_ride_count = Count('rides', distinct=True)annotation in views - Updated templates to use
current_ride_countfor display
Future Considerations
We might want to:
- Add a periodic task to sync the stored
ride_countwith the computed value - Consider deprecating the stored field if it's not needed for performance
- Add validation to ensure the stored count stays in sync with reality
Related Files
- parks/models.py
- parks/views.py
- parks/templates/parks/partials/park_list_item.html
- parks/tests/test_filters.py