问题
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