Django:在 models.py 之外声明基本模型时出现 app_label 问题
我有一个抽象的 Container 类,它允许派生模型保存一些内容块,例如图像、文本等,它们也是单独的模型。为了数据库的整洁,我希望这些模型的表被标记为 content_block_image、content_block_text 等。
但是当我在内容模型的元类中指定 app_label = 'content_block'
时,我在syncdb期间收到错误:
content.event:“content”与模型 Content 具有 m2m 关系,该模型尚未安装或者是抽象的。
我声明以下基类,如下所示:
# base.py
class Content(models.Model):
tags = models.TextField(_('tags'), blank=True)
user = models.ForeignKey(User)
content_type = models.ForeignKey(ContentType,
related_name='%(class)s_content_set')
container_type = models.ForeignKey(ContentType)
container_id = models.PositiveIntegerField()
container = generic.GenericForeignKey('container_type', 'container_id')
class Meta:
app_label = 'content_block'
class Container(models.Model):
content_type = models.ForeignKey(ContentType,
related_name='%(class)s_container_set')
content = generic.GenericRelation('Content',
content_type_field='container_type',
object_id_field='container_id')
class Meta:
abstract = True
然后,在我的模型中,我声明称为容器的模型,例如:
# models.py
class Event(Container):
title = models.CharField(max_length=100)
start = models.DateTimeField()
end = models.DateTimeField()
如果我删除 app_label
,syncdb 运行就不会出现问题。看来app_label不仅仅是一个标签。
关于如何使用内容基类集的 app_label 来实现这一点有什么想法吗?
I have an abstract Container class which allows derived models to hold some content blocks such as images, text, etc., which are also separate models. For db tidiness, I want tables for those models to be labeled as content_block_image, content_block_text, etc.
But when I specify app_label = 'content_block'
in Meta class of Content model, I am getting an error during syncdb:
content.event: 'content' has an m2m relation with model Content, which has either not been installed or is abstract.
I am declaring the following base classes as follows:
# base.py
class Content(models.Model):
tags = models.TextField(_('tags'), blank=True)
user = models.ForeignKey(User)
content_type = models.ForeignKey(ContentType,
related_name='%(class)s_content_set')
container_type = models.ForeignKey(ContentType)
container_id = models.PositiveIntegerField()
container = generic.GenericForeignKey('container_type', 'container_id')
class Meta:
app_label = 'content_block'
class Container(models.Model):
content_type = models.ForeignKey(ContentType,
related_name='%(class)s_container_set')
content = generic.GenericRelation('Content',
content_type_field='container_type',
object_id_field='container_id')
class Meta:
abstract = True
Then, in my models I am declaring models I call container such as:
# models.py
class Event(Container):
title = models.CharField(max_length=100)
start = models.DateTimeField()
end = models.DateTimeField()
If I remove the app_label
syncdb runs without a problem. It seems that app_label is not just a label.
Any ideas on how to get this going with the app_label for the Content base class set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自文档
content_block
application contains ?如果没有,我不确定它会起作用。看来你想做的是强制表名。这家酒店有可能
From the doc
content_block
application exists ? if not, i'm not shure it will work.It seems that what you want to do is forcing the table names. It's possible will this property