Opened 11 years ago

Closed 6 years ago

#607 closed enhancement (wontfix)

Admin option to show/hide fields

Reported by: wilson Owned by: nobody
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by wilson)

Concept: An admin option that specifies a list of fields to show/hide based on the value of another field.

Example: An "I like cake" checkbox that, when checked, shows two previously hidden text fields "Favorite cake" and "Favorite frosting".

Code Example:

admin.switch = [('likes_cake', {'True': ['favorite_cake', 'favorite_frosting']})]

You could also tie this to a particular value of a select field:

admin.switch = [('gender', {'M': ['favorite_sport'], 'F': ['favorite_soap_opera']})]

Unsolved issues:

  • Need some place to specify the initial state of the targeted fields (visible or hidden). Default (and most common case) would be hidden, but there are cases where this needs to be reversed.
  • All hiding and should happen in javascript, so fields don't get hidden that can't be turned back on with javascript disabled. The switch option could also tie into the validation framework so unnecessary fields (meant to be hidden) don't get filled in even when javascript is off.
  • Any other ideas?

Change History (11)

comment:1 Changed 11 years ago by adrian

To be consistent, the examples should both take dictionaries. (And they should take a list, so that you can have more than one switch per admin screen.)

admin.switch = [('likes_cake', {'True': ['favorite_cake', 'favorite_frosting']})]
admin.switch = [('gender', {'M': ['favorite_sport'], 'F': ['favorite_soap_opera']})] 

It's a bit syntax heavy, but I like the idea.

comment:2 Changed 11 years ago by wilson

  • Description modified (diff)

comment:3 Changed 10 years ago by anonymous

  • Component changed from Admin interface to Translations
  • milestone set to Version 1.1
  • Owner changed from adrian to hugo
  • priority changed from normal to highest
  • Severity changed from trivial to blocker
  • Type changed from enhancement to defect
  • Version set to magic-removal

comment:4 Changed 10 years ago by hugo

  • Component changed from Translations to Admin interface
  • Owner changed from hugo to adrian

comment:5 Changed 10 years ago by adrian

  • priority changed from highest to normal
  • Severity changed from blocker to normal

comment:6 Changed 10 years ago by anonymous

  • Type changed from defect to enhancement

comment:7 Changed 9 years ago by anonymous

Milestone Version 1.1 deleted

comment:8 Changed 9 years ago by SmileyChris

  • Summary changed from IDEA: admin option to show/hide fields to Admin option to show/hide fields
  • Triage Stage changed from Unreviewed to Accepted
  • Version magic-removal deleted

And while I'm here, my take on this:

class Person(models.Model):
    gender = models.CharField(maxlength=1)
    likes_cake = models.BooleanField()
    favorite_cake = models.CharField(maxlength=30, blank=True)
    favorite_sport = models.CharField(maxlength=50, blank=True)
    favorite_soap = models.CharField(maxlength=50, blank=True)
    sport_fanatic = models.BooleanField()
    class Admin:
        hide_unless = {
            'favorite_cake': {'likes_cake': True},
            'favorite_sport': {'gender': 'M'},
            'favorite_soap': {'gender': 'F'},
            'sport_fanatic': {'gender': 'M', 'favorite_sport': True},
  • Can optionally be matched against a list (eg 'icing': {'favourite_cake': ['carrot', 'banana']})
  • Multiple options (like sport_fanatic above) are anded, could potentially be passed a list of dictionaries for ors
  • show_unless could also be implemented for the more rare "show until" condition

comment:9 Changed 9 years ago by ubernostrum

  • Resolution set to wontfix
  • Status changed from new to closed

In theory, once newforms-admin lands you'll have a level of customization available which will remove the need for the admin app to directly support this.

comment:10 Changed 6 years ago by anonymous

  • Resolution wontfix deleted
  • Status changed from closed to reopened

Hi - I'm reopening this because the rationale for closing it was that it would be possible to do with newforms... As far as I can tell this isn't straightforward with the current admin, although I'd be happy to stand corrected.

I can think of a number of use-cases for this, and am about to start very inexpertly hacking the admin page javascript for want of a better solution.


comment:11 Changed 6 years ago by carljm

  • Resolution set to wontfix
  • Status changed from reopened to closed

Please don't just reopen tickets closed by a core developer; bring it up on the django-developers mailing list instead.

This is quite possible to build in a generic and reusable way external to Django, using a ModelForm and/or a ModelAdmin subclass and the forms.Media framework, so there is no need for Django to include it (and if Django ever did include it, it would be because it first got very popular as an external project).

"How" questions should go to #django, django-users, etc.

Note: See TracTickets for help on using tickets.
Back to Top