how to hide or show fields of model after clicked on choice field from admin in django

回眸只為那壹抹淺笑 提交于 2021-02-08 03:34:50

问题


class BannerAddPage(models.Model):
    BOOL_CHOICES = ((True, 'Live'), (False, 'Pause'))
    TYPE_CHOICES = (('text', 'text'), ('image', 'image'))
    category=models.ForeignKey(Category,blank=True)
    client=models.ForeignKey(Client)
    ad_type=models.CharField(choices=TYPE_CHOICES,default=True)
    ad_name=models.CharField(max_length=800,null=True,blank=True)
    ad_title=models.CharField(max_length=800,null=True,blank=True)
    ad_url1=models.URLField(max_length=800,blank=True,null=True)
    ad_banner2=models.ImageField(upload_to=upload_to1,blank=True,null=True)

See the image here

I want to hide some fields after click on ad_type(choice) field.


回答1:


Best way is to use form media to add an external javascript file that will listen to the change event of the field ad_type and perform the action you need.

In this case you will need little python and lot more of js, but I think is the clean way.

To add the external js file use the class Media, this answer to another question shows a practical example.

Than you just have to have fun with writing js code, you can use the jQuery already included in django admin.

Hope this helps



来源:https://stackoverflow.com/questions/35289501/how-to-hide-or-show-fields-of-model-after-clicked-on-choice-field-from-admin-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!