在Django的管理员中单击选择字段后如何隐藏或显示模型的字段 [英] how to hide or show fields of model after clicked on choice field from admin in django

查看:58
本文介绍了在Django的管理员中单击选择字段后如何隐藏或显示模型的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class BannerAddPage(models.Model)类:BOOL_CHOICES =((真,'实时'),(假,'暂停'))TYPE_CHOICES =((('文本','文本'),('图像','图像')))category = models.ForeignKey(Category,blank = True)client = models.ForeignKey(客户)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) 

在此处查看图片

我想在点击 ad_type(choice)字段后隐藏一些字段.

解决方案

最好的方法是使用表单媒体添加一个外部javascript文件,该文件将侦听ad_type字段的change事件并执行所需的操作.

在这种情况下,您将需要很少的python和更多的js,但我认为这是干净的方法.

要添加外部js文件,请使用媒体类,此 answer 可以另一个问题显示了一个实际例子.

除了只需要编写js代码之外,您还可以使用django admin中已包含的jQuery.

希望这会有所帮助

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.

解决方案

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

这篇关于在Django的管理员中单击选择字段后如何隐藏或显示模型的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆