简体   繁体   English

管理员Django字段排除下拉列表

[英]admin django field exclude dropdown

I have the following model 我有以下型号

class Consultation(models.Model):
    id = models.AutoField('Id', primary_key=True)
    date = models.DateTimeField('date')
    services = models.CharField('services', choices=SERVICES, max_length=200, null=True, blank=True)
    weight = models.DecimalField('weight', decimal_places=1, max_digits=10)
    size = models.DecimalField('size', decimal_places=1, max_digits=10)
    imc = models.DecimalField('size', decimal_places=1, max_digits=10)

I have the field "service", in which three options ('a', 'b', 'c') are deployed. 我有“服务”字段,其中部署了三个选项(“ a”,“ b”,“ c”)。

my question is: is it possible that when you create a record of this model (add), selecting for example the option "a" field "service", the 'imc' field is excluded? 我的问题是:是否有可能在创建此模型的记录(添加)时,例如选择选项“ a”字段“服务”,而排除“ imc”字段?

I tested with the method "get_form" but does not work when I want to add a record only when already registered. 我使用方法“ get_form”进行了测试,但是当我只想在已经注册的情况下才想添加记录时,它不起作用。

thanks for the help! 谢谢您的帮助!

You can do the exclude validations in the clean method of your Consultation class. 您可以在Consultation类的clean方法中进行排除验证。 Check the below link and you can implement a similar validation like the following 检查以下链接,您可以实施类似以下的验证

if services == 'a':
    self.fields['imc'].required = False

https://stackoverflow.com/a/7093995/4757102 https://stackoverflow.com/a/7093995/4757102

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM