简体   繁体   English

如何仅在django-admin中选择下拉菜单中的特定选项时才显示字段

[英]how to display fields only when particular option in drop down is selected in django-admin

py looks as below, py如下所示,

     class Billing_Detail(models.Model):
        bill_choices = [('Permanent','Permanent'),('Temporary','Temporary')]
        emp_name = models.ForeignKey(Employee)
        bill_type = models.CharField(max_length = 10,choices=bill_choices,verbose_name='Bill type')
        start_date = models.DateField(blank=True,verbose_name='Billing Start Date',null=True)
        end_date = models.DateField(blank=True,verbose_name='Billing End Date',null=True)

Now in admin page I need to show start_date and end_date field only if the user choose bill_choice as Temporary. 现在,在管理页面中,仅当用户选择bill_choice作为“临时”时,我才需要显示start_date和end_date字段。 How can i do that. 我怎样才能做到这一点。 Do i need to write a java-script for it if so how can i register the java script with admin page in django 如果需要,我是否需要为其编写Java脚本吗?如何在Django的管理页面中注册Java脚本?

This is the closest I got: 这是我最近得到的:

class BillingDetailAdmin(admin.ModelAdmin):
    class Media:
        js = ('app/js/code.js', )  # Django will resolve the static URL on its own

Then in the page, I found jQuery to be bound to grp.jQuery or django.jQuery (unless you include your own jQuery there's the one that comes with Django admin), then I listen on dropdown changes then show/hide inputs accordingly. 然后在页面中,我发现jQuery绑定到grp.jQuerydjango.jQuery (除非您包括自己的jQuery,否则Django admin附带了jQuery),然后我听下拉列表更改,然后相应地显示/隐藏输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM