[英]Add model attribute to same model in Django
I have a model called Company.我有一个名为 Company 的 model。 The Company could be the child of a bigger company.
该公司可能是一家更大公司的子公司。 So in the model Company should be a attribute "parent" that is also a Company.
所以在 model 中,公司应该是一个属性“父”,它也是一个公司。
I got this:我懂了:
class Company(models.Model):
name = models.CharField(max_length=250)
parent = models.ForeignKey(
Company,
on_delete=models.SET_NULL,
related_name="notification",
null=True,
blank=False,
)
But django is always saying I need to create a Company class.但是 django 总是说我需要创建一个公司 class。 Is this the right way to do this?
这是正确的方法吗?
Use 'self' keyword to reference the same model.使用“self”关键字来引用相同的 model。
class Company(models.Model):
name = models.CharField(max_length=250)
parent = models.ForeignKey(
'self',
on_delete=models.SET_NULL,
related_name="notification",
null=True,
blank=False,
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.