【发布时间】:2017-10-14 18:36:06
【问题描述】:
我尝试创建一些模型。我实际上查看了一些示例,但大多数都不起作用。示例:
class Piece(models.Model):
name = models.CharField(max_length=100)
class Meta:
abstract = True
class Article(Piece):
pass
class Book(Piece):
pass
class Monday(Book, Article):
pass
class Tuesday(Book, Article):
pass
所以我的目标是通过这样的方式获得价值 -> Monday.Article.name。
我希望每个工作日都有不同的表格,其中包含文章的名称。这是我得到的错误:
ERRORS:
Testing.Monday: (models.E005) The field 'id' from parent model 'Testing.book' clashes with the field 'id' from parent model 'Testing.article'.
Testing.Monday: (models.E005) The field 'name' from parent model 'Testing.book' clashes with the field 'name' from parent model 'Testing.article'.
Testing.Tuesday: (models.E005) The field 'id' from parent model 'Testing.book' clashes with the field 'id' from parent model 'Testing.article'.
Testing.Tuesday: (models.E005) The field 'name' from parent model 'Testing.book' clashes with the field 'name' from parent model 'Testing.article'.
看起来我的文章和书籍使用了相同的名称。这是如何工作的?
编辑:使用这个:
class Article(Piece):
article_id = models.AutoField(primary_key=True)
class Book(Piece):
book_id = models.AutoField(primary_key=True)
会导致这个错误:
Testing.Monday: (models.E005) The field 'piece_ptr' from parent model
'Testing.book' clashes with the field 'piece_ptr' from parent model
'Testing.article'.
唯一的解决方案是:
article_to_piece = models.OneToOneField(Piece, parent_link=True)但这不会生成日期和书籍/文章之间的链接。我只能为这一天添加一个名字。
【问题讨论】:
-
这是一个非常糟糕的主意。只需使用带有“日”字段的单个表。
-
好吧,我想我必须按照你的方式去做!你能解释一下为什么我的方法不好吗?除了它不起作用
标签: python django django-models model