我安装了django应用,并希望限制外键的选择:
class InstalledModel(models.Model):
...
base_field = models.ForeignKey(AnotherModel)
class MyModel(InstalledModel):
"""
Somehow use limit_choices_to in base_field
"""
我知道不能在继承的模型中更改字段,但是想知道它的参数是否也是如此,因为它似乎并没有改变模型本身的序列化和初始化。
您可以尝试使用属性作为limit_choices_to
参数,例如:
class InstalledModel(models.Model):
@property
def another_model_choices(self):
if isinstance(self, MyModel):
return {'some_condition': True}
return None
base_field = models.ForeignKey(AnotherModel, limit_choices_to=another_model_choices)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句