In the Django admin, when the choice Custom
is selected from a dropdown list, I want to display the inline start_date
and end_date
fields to allow the user to specify a specific start and end-date instead of a pre-defined time period.
After researching for some time, suggestions include: use hidden fields, define override get_form in ModelAdmin, or use custom Javascript (which I have zero experience with).
The Question: how can I display (show) the inline start_date
and end_date
fields when a specific value (Custom
) is selected in the dropdown of a Django Admin field? When Custom
is not selected, start_date
and end_date
would be hidden from view.
Step 1:
Step 2:
Step 3:
Below is a complete example of the exact example code I have locally:
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
...
'dropdown.apps.DropdownConfig',
]
apps.py
from django.apps import AppConfig
class DropdownConfig(AppConfig):
name = 'dropdown'
models.py
from django.db import models
class DropdownModel(models.Model):
CHOICES = (
('Today', 'Today'),
('Yesterday', 'Yesterday'),
('Last 7 Days', 'Last 7 Days'),
('Last 14 Days', 'Last 14 Days'),
('Last 30 Days', 'Last 30 Days'),
('Last 60 Days', 'Last 60 Days'),
('Last 90 Days', 'Last 90 Days'),
('This Year', 'This Year'),
('All Time', 'All Time'),
('Custom', 'Custom')
)
date_range = models.CharField(max_length=15)
start_date = models.DateField()
end_date = models.DateField()
forms.py
from django import forms
from dropdown.models import DropdownModel
class DropdownModelForm(forms.ModelForm):
class Meta:
model = DropdownModel
fields = ('date_range',)
widgets = {
'date_range': forms.Select(choices=DropdownModel.CHOICES)
}
admin.py
from django.contrib import admin
from dropdown.models import DropdownModel
from dropdown.forms import DropdownModelForm
class DropdownModelAdmin(admin.ModelAdmin):
fieldsets = (
('Date Range', {
'fields': ('date_range', ('start_date', 'end_date'))
}),
)
form = DropdownModelForm
admin.site.register(DropdownModel, DropdownModelAdmin)
See Question&Answers more detail:
os