Implement ride count fields with real-time annotations; update filters and templates for consistency and accuracy

This commit is contained in:
pacnpal
2025-02-13 16:44:30 -05:00
parent 9d6f6dab2c
commit c19aaf2f4b
10 changed files with 988 additions and 367 deletions

View File

@@ -0,0 +1,59 @@
# 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:
1. **Model Fields**:
- `ride_count`: Stored count of all rides
- `coaster_count`: Stored count of roller coasters
- Used in models and database schema
- Required for backward compatibility
2. **Annotations**:
- `current_ride_count`: Real-time count of all rides
- `current_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
1. Views:
- Added base queryset method with annotations
- Used 'current_' prefix for annotated counts
- Ensured all views use the base queryset
2. Filters:
- Updated filter fields to use annotated counts
- Configured filter class to always use base queryset
- Maintained filter functionality with new field names
3. Templates:
- Updated templates to use computed counts
## Why This Pattern
1. **Consistency**: Using the 'current_' prefix clearly indicates which values are computed in real-time
2. **Compatibility**: Maintains support for existing code that relies on the stored fields
3. **Flexibility**: Allows gradual migration from stored to computed counts if desired
4. **Performance Option**: Keeps the option to use stored counts for expensive queries
## Future Considerations
We might want to:
1. Add periodic tasks to sync stored counts with computed values
2. Consider deprecating stored fields if they're not needed for performance
3. Add validation to ensure stored counts stay in sync with reality
4. 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