title: Django Admin Show / Hide Fields If Specific Value Is Selected In A Dropdown
body: <p>In the Django admin, when the choice <code>Custom</code> is selected from a dropdown list, I want to display the inline <code>start_date</code> and <code>end_date</code> fields to allow the user to specify a specific start and end-date instead of a pre-defined time period.</p>
  
  <p>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).</p>
  
  <p><strong>The Question</strong>: how can I display (show) the inline <code>start_date</code> and <code>end_date</code> fields when a specific value (<code>Custom</code>) is selected in the dropdown of a Django Admin field? When <code>Custom</code> is not selected, <code>start_date</code> and <code>end_date</code> would be hidden from view.</p>
  
  <hr>
  
  <p><strong>Step 1:</strong>
  <a href="https://i.stack.imgur.com/KIQUd.png" rel="noreferrer"><img src="https://i.stack.imgur.com/KIQUd.png" alt="step 1 - select a date range"></a></p>
  
  <p><strong>Step 2:</strong>
  <a href="https://i.stack.imgur.com/FvZfp.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FvZfp.png" alt="step 2 - if custom is selected"></a></p>
  
  <p><strong>Step 3:</strong>
  <a href="https://i.stack.imgur.com/Tcjwi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Tcjwi.png" alt="step 3 - show the inline custom date fields"></a></p>
  
  <hr>
  
  <p>Below is a complete example of the exact example code I have locally:</p>
  
  <p><strong>settings.py</strong></p>
  
  <pre><code>INSTALLED_APPS = [
      'django.contrib.admin',
      ...
      'dropdown.apps.DropdownConfig',
  ]
  </code></pre>
  
  <p><strong>apps.py</strong></p>
  
  <pre><code>from django.apps import AppConfig
  
  class DropdownConfig(AppConfig):
      name = 'dropdown'
  </code></pre>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>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()
  </code></pre>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>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)
          }
  </code></pre>
  
  <p><strong>admin.py</strong></p>
  
  <pre><code>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)
  </code></pre>