Django:在 models.py 之外声明基本模型时出现 app_label 问题

发布于 2024-10-13 13:05:41 字数 1559 浏览 2 评论 0原文

我有一个抽象的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只怪假的太真实 2024-10-20 13:05:41

来自文档

如果模型存在于标准 models.py 之外(例如,如果应用程序的模型位于 myapp.models 的子模块中),则该模型必须定义它属于哪个应用程序:

app_label = 'myapp'

content_block application contains ?如果没有,我不确定它会起作用。

看来你想做的是强制表名。这家酒店有可能

用于模型的数据库表的名称:

db_table = '音乐专辑'

From the doc

If a model exists outside of the standard models.py (for instance, if the app’s models are in submodules of myapp.models), the model must define which app it is part of:

app_label = 'myapp'

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

The name of the database table to use for the model:

db_table = 'music_album'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文