Django admin:显示/隐藏选择任何一个下拉值的字段

时间:2017-03-26 11:46:26

标签: jquery django django-models django-forms django-admin

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)

在管理页面中,我有type_of_plan选项:" Global"," User Specific" ...当我选择" User Specific"值我需要显示specific_users字段当我选择" Global"值我需要隐藏specific_users字段......我该怎么做?

1 个答案:

答案 0 :(得分:0)

admin.py创建班级PlanAdmin(admin.ModelAdmin)中,实现您在get_form函数中获取管理员表单的逻辑。

有关详细信息和示例实现,请参阅此更一般的问题:How to hide some fields in django-admin?