1

models.py:

class Plan(models.Model):
    PLAN_TYPE_CHOICES = (
        ('global', 'Global'),
        ('user specific', 'User Specific'),
    )
    name_of_plan = models.CharField(max_length=256, null=False, blank=False)
    number_of_months = models.IntegerField(db_index=True)
    type_of_plan = models.CharField(choices=PLAN_TYPE_CHOICES)
    specific_users = models.ManyToManyField(User, db_index=True)

admin.py

from django.contrib import admin
from .models import Plan

class PlanAdmin(admin.ModelAdmin):
    model = Plan

admin.site.register(Plan, PlanAdmin)

In admin page I have type_of_plan with choices: "Global", "User Specific"... When I select "User Specific" value I need to show specific_users field And when I select "Global" value I need to hide specific_users field... How can I do it?

Maulik Patel
  • 572
  • 5
  • 10

1 Answers1

0

In the admin.py create class PlanAdmin(admin.ModelAdmin) and implement your logic for getting admin form in the get_form function.

For details and example implementation, see this more general question: How to hide some fields in django-admin?

Community
  • 1
  • 1
matusko
  • 3,487
  • 3
  • 20
  • 31
  • [How to hide some fields in django-admin?](http://stackoverflow.com/questions/10330916/how-to-hide-some-fields-in-django-admin) This solution doesn't work for me because my question is "Is possible to show and hide some fields in admin panel based on choice in type_of_plan field (using JQuery or something else)?" – Maulik Patel Mar 26 '17 at 17:20
  • you can implement that logic in the `get_form` function – matusko Mar 26 '17 at 19:59