开发者

Django Admin: Can I create a field that can only be used if a specific choice has been picked in a previous field on the same model?

I would like to be able to set access to one field based on what was chosen in a previous field on the same model. Ideally so it can be used in the admin interface.

My model looks like so:

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

Ca开发者_高级运维n I restrict access, specifically in the admin, to the is_castrated field so that it is only available when the user has chosen Male from the gender field?


Sounds like your going to need some javascript. You can do this by adding a bit to your admin.py file. Check out this:

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",)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜