Django Admin:我可以创建一个只有在同一模型上的前一个字段中选择了特定选项才能使用的字段吗?

2024-09-30 18:19:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想能够设置访问一个领域的基础上选择的前一个领域在同一个模型。理想情况下,它可以在管理界面中使用。在

我的模型看起来是这样的:

GENDER_CHOICES = (
    ('f', 'Female'),
    ('m', 'Male'),
)

class Animal(models.Model):
    name = models.CharField(max_length=255)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    is_castrated = models.BooleanField()

    def __unicode__(self):
        return self.name

我是否可以限制对is_castrated字段的访问,使其仅在用户从gender字段中选择了Male时才可用?在


Tags: name模型selfismodelsgenderlength基础
1条回答
网友
1楼 · 发布于 2024-09-30 18:19:49

听起来你需要一些javascript。 你可以通过在管理员py文件。 看看这个:

ModelAdmin Media Defniitions - There are times where you would like add a bit of CSS and/or JavaScript to the add/change views. This can be accomplished by using a Media inner class on your ModelAdmin:

class ArticleAdmin(admin.ModelAdmin):
    class Media:
        css = {
             "all": ("my_styles.css",)
         }
        js = ("my_code.js",)

相关问题 更多 >