IMPORTANT: Before implementing features or exploring the codebase, read the knowledgebase.md file to understand:
- Technology stack (Django 5.1.8, Python 3.11, PostgreSQL)
- Project structure (studies, uncontrast_studies apps)
- API architecture (DRF, JWT, multi-SPA)
- Testing framework (pytest, Django Testing)
- CI/CD pipeline (GitHub Actions, Heroku)
- Deployment configuration (Docker, Heroku)
- Development practices (ruff, service layers)
When implementing changes, always run tests with the Testing configuration:
DJANGO_CONFIGURATION=Testing python manage.py testWhen working with Django admin files (**/admin.py, **/admin/*.py), follow these standards:
- Create and use base admin classes for shared functionality
- Use mixins (e.g., ImportExportMixin) for specific features
- Inherit from
ModelAdminfor standard admin functionality - Use custom base classes for project-wide admin features
- Register all models in their respective app's
admin.py - Use descriptive admin class names (e.g.,
UserAdmin,ProductAdmin) - Group related models together in the admin file
- Use consistent ordering of model registrations
- Prefer the
@admin.register(Model)decorator for cleaner registration
- Define
list_displayfirst - Follow with
list_filter - Then define
search_fields - Add
filter_horizontalfor many-to-many fields - Define
inlinesat the end - Use consistent field ordering in
list_display - Group related attributes together
- Refrain from using properties in
list_displayunless they are part of aselect_relatedquery
- Create base inline classes for common inline patterns
- Set
show_change_link = Truefor inlines when appropriate - Set
extra = 0for inlines to prevent empty forms - Use
StackedInlineorTabularInlineas appropriate - Define
fk_namewhen needed for complex relationships - Use
min_numandmax_numfor inline limits
- Create custom filters by inheriting from
admin.SimpleListFilter - Use
RelatedOnlyFieldListFilterfor foreign key fields - Use
NumericRangeFilterfor numeric fields - Group related filters together
- Use descriptive filter titles
- Implement efficient queryset methods
- Create custom forms for complex validation
- Define form fields in the
Metaclass - Add custom validation in
clean() - Use consistent field ordering
- Add helpful
help_textfor fields - Use appropriate widgets for field types
- Use django-import-export for data import/export
- Create custom resource classes for complex models
- Define export formats (e.g., CSV, XLSX)
- Implement custom export logic when needed
- Handle related model exports properly
- Add export actions to admin classes
- Override
get_queryset()for performance optimization - Use
select_related()for foreign key relationships - Use
prefetch_related()for many-to-many relationships - Document complex queryset modifications
- Implement efficient filtering
- Use database indexes appropriately
- Define admin actions as functions with
@admin.actiondecorator - Use descriptive action names
- Add confirmation for destructive actions
- Handle bulk operations efficiently
- Implement proper error handling
- Add action permissions when needed
- Use
@admin.displaydecorator for custom display methods - Handle empty values appropriately
- Keep display methods simple and focused
- Document complex display logic
- Use consistent formatting
- Implement efficient display methods
@admin.register(Model)
class ModelAdmin(BaseAdmin):
list_display = ('id', 'name', 'related_field')
list_filter = ('field1', 'field2')
search_fields = ('name', 'description')
filter_horizontal = ('many_to_many_field',)
inlines = [RelatedInline]class RelatedInline(admin.StackedInline):
model = RelatedModel
fields = ('field1', 'field2')
show_change_link = True
extra = 0
min_num = 1
max_num = 5class CustomFilter(admin.SimpleListFilter):
title = "Filter Title"
parameter_name = "filter_name"
def lookups(self, request, model_admin):
return [
('value1', 'Label 1'),
('value2', 'Label 2'),
]
def queryset(self, request, queryset):
if self.value():
return queryset.filter(field=self.value())
return queryset- Optimize database queries (use
select_related,prefetch_related) - Use
readonly_fieldsand permission checks for security - Keep admin classes focused and well-documented
- Use type hints and write unit tests
- Provide helpful
help_textand clear error messages - Use consistent formatting and naming
- Group related models and imports together
- Follow PEP 8 guidelines (120-char line length via ruff)
- Regularly review and update admin code
- Run
ruff check . --fixbefore committing - Run
ruff formatfor consistent formatting - Ensure all tests pass with
DJANGO_CONFIGURATION=Testing python manage.py test