![](/img/trans.png)
[英]Migrate from one django model to two models referenced with a foreign key
[英]Django: How to define the models when parent model has two foreign keys come from one same model?
我想在父模型中定義兩個模型字段:created_by,modified_by,它們將用作子模型的公共字段。
class ExtendedModel(models.Model):
created_by = models.ForeignKey(User,related_name='r_created_by')
modified_by = models.ForeignKey(User,related_name='r_modified_by')
class Meta:
abstract = True
class ChildModel1(ExtendedModel):
pass
class ChildModel2(ExtendedModel):
pass
這會產生錯誤,因為ChildModel1和ChildModel2在created_by和modified_by字段上具有related_name沖突。
Django文檔說明了如何解決此問題: http : //docs.djangoproject.com/en/dev/topics/db/models/#abstract-related-name
class ExtendedModel(models.Model):
created_by = models.ForeignKey(User,related_name='"%(app_label)s_%(class)s_created_by')
modified_by = models.ForeignKey(User,related_name='"%(app_label)s_%(class)s_modified_by')
class Meta:
abstract = True
class ChildModel1(ExtendedModel):
pass
class ChildModel2(ExtendedModel):
pass
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.