参考基本抽象模型中的子 class 外键
Refer to child class foreign key in base abstract model
我正在尝试访问基本抽象模型中的子模型外键,因此我不必在每个子模型中重复外键。
这是我的代码:
class BaseModel(models.Model):
child_field = models.ForeignKey(to='child_class_either_ModelA_OR_ModelB')
class Meta:
abstract = True
class ModelA(BaseModel):
....
class ModelB(BaseModel):
....
我想引用基本抽象模型中的子模型。
有没有办法在基础模型中使用子模型?
因为摘要中的字段 BaseModel
将是 inherited, then we could have ModelA.child_field
that references itself (ModelA
) and ModelB.child_field
that references itself (ModelB
) by declaring a ForeignKey
that references itself as documented here:
To create a recursive relationship – an object that has a many-to-one relationship with itself – use models.ForeignKey('self', on_delete=models.CASCADE)
.
class BaseModel(models.Model):
child_field = models.ForeignKey(to='self', on_delete=models.CASCADE, null=True)
class Meta:
abstract = True
class ModelA(BaseModel):
field_a = models.CharField(max_length=200)
class ModelB(BaseModel):
field_b = models.CharField(max_length=200)
>>> from my_app.models import *
>>>
>>> model_a_1 = ModelA.objects.create(field_a="Value 1")
>>> model_a_2 = ModelA.objects.create(field_a="Value 2", child_field=model_a_1)
>>> ModelA.objects.values()
<QuerySet [{'id': 1, 'child_field_id': None, 'field_a': 'Value 1'}, {'id': 2, 'child_field_id': 1, 'field_a': 'Value 2'}]>
>>>
>>> model_b_3 = ModelB.objects.create(field_b="Value 3")
>>> model_b_4 = ModelB.objects.create(field_b="Value 4", child_field=model_b_3)
>>> ModelB.objects.values()
<QuerySet [{'id': 1, 'child_field_id': None, 'field_b': 'Value 3'}, {'id': 2, 'child_field_id': 1, 'field_b': 'Value 4'}]>
不同子模型之间的交叉引用会失败,例如ModelA
链接到 ModelB
。
>>> model_a_5 = ModelA.objects.create(field_a="Value 5", child_field=model_b_4)
Traceback (most recent call last):
raise ValueError(
ValueError: Cannot assign "<ModelB: ModelB object (2)>": "ModelA.child_field" must be a "ModelA" instance.
>>>
>>> model_b_6 = ModelB.objects.create(field_b="Value 6", child_field=model_a_2)
Traceback (most recent call last):
raise ValueError(
ValueError: Cannot assign "<ModelA: ModelA object (2)>": "ModelB.child_field" must be a "ModelB" instance.
我正在尝试访问基本抽象模型中的子模型外键,因此我不必在每个子模型中重复外键。
这是我的代码:
class BaseModel(models.Model):
child_field = models.ForeignKey(to='child_class_either_ModelA_OR_ModelB')
class Meta:
abstract = True
class ModelA(BaseModel):
....
class ModelB(BaseModel):
....
我想引用基本抽象模型中的子模型。
有没有办法在基础模型中使用子模型?
因为摘要中的字段 BaseModel
将是 inherited, then we could have ModelA.child_field
that references itself (ModelA
) and ModelB.child_field
that references itself (ModelB
) by declaring a ForeignKey
that references itself as documented here:
To create a recursive relationship – an object that has a many-to-one relationship with itself – use
models.ForeignKey('self', on_delete=models.CASCADE)
.
class BaseModel(models.Model):
child_field = models.ForeignKey(to='self', on_delete=models.CASCADE, null=True)
class Meta:
abstract = True
class ModelA(BaseModel):
field_a = models.CharField(max_length=200)
class ModelB(BaseModel):
field_b = models.CharField(max_length=200)
>>> from my_app.models import *
>>>
>>> model_a_1 = ModelA.objects.create(field_a="Value 1")
>>> model_a_2 = ModelA.objects.create(field_a="Value 2", child_field=model_a_1)
>>> ModelA.objects.values()
<QuerySet [{'id': 1, 'child_field_id': None, 'field_a': 'Value 1'}, {'id': 2, 'child_field_id': 1, 'field_a': 'Value 2'}]>
>>>
>>> model_b_3 = ModelB.objects.create(field_b="Value 3")
>>> model_b_4 = ModelB.objects.create(field_b="Value 4", child_field=model_b_3)
>>> ModelB.objects.values()
<QuerySet [{'id': 1, 'child_field_id': None, 'field_b': 'Value 3'}, {'id': 2, 'child_field_id': 1, 'field_b': 'Value 4'}]>
不同子模型之间的交叉引用会失败,例如ModelA
链接到 ModelB
。
>>> model_a_5 = ModelA.objects.create(field_a="Value 5", child_field=model_b_4)
Traceback (most recent call last):
raise ValueError(
ValueError: Cannot assign "<ModelB: ModelB object (2)>": "ModelA.child_field" must be a "ModelA" instance.
>>>
>>> model_b_6 = ModelB.objects.create(field_b="Value 6", child_field=model_a_2)
Traceback (most recent call last):
raise ValueError(
ValueError: Cannot assign "<ModelA: ModelA object (2)>": "ModelB.child_field" must be a "ModelB" instance.