简体   繁体   English

如何使一个字段在模型中依赖于另一个字段? Django的

[英]How to make one field dependable of another field in models? Django

How to make one field dependable on another? 如何使一个字段依赖于另一个字段? for example 例如

class Example(models.Model):
    question = models.BooleanField()

then if question is True: 那么如果问题为真:

    example1 = models.ForeignKey('Object1')

when false: 如果为假:

    example2 = models.ForeignKey('Object2')

or something like that? 或类似的东西? Thanks for help. 感谢帮助。

My suggestion would be to use a Generic Foreign Key which allows for polymorphic types. 我的建议是使用允许多态类型的通用外键 This avoids the extra column. 这样可以避免多余的列。

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

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