Opened 13 years ago

Closed 8 years ago

#607 closed enhancement (wontfix)

Admin option to show/hide fields

Reported by: Wilson Miner 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 Miner)

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 13 years ago by Adrian Holovaty

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 13 years ago by Wilson Miner

Description: modified (diff)

comment:3 Changed 12 years ago by anonymous

Component: Admin interfaceTranslations
milestone: Version 1.1
Owner: changed from Adrian Holovaty to hugo
priority: normalhighest
Severity: trivialblocker
Type: enhancementdefect
Version: magic-removal

comment:4 Changed 12 years ago by hugo

Component: TranslationsAdmin interface
Owner: changed from hugo to Adrian Holovaty

comment:5 Changed 12 years ago by Adrian Holovaty

priority: highestnormal
Severity: blockernormal

comment:6 Changed 11 years ago by anonymous

Type: defectenhancement

comment:7 Changed 11 years ago by (none)

Milestone Version 1.1 deleted

comment:8 Changed 11 years ago by Chris Beaven

Summary: IDEA: admin option to show/hide fieldsAdmin option to show/hide fields
Triage Stage: UnreviewedAccepted
Version: magic-removal

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 11 years ago by James Bennett

Resolution: wontfix
Status: newclosed

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 8 years ago by anonymous

Resolution: wontfix
Status: closedreopened

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 8 years ago by Carl Meyer

Resolution: wontfix
Status: reopenedclosed

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