【问题标题】:Install Django Taggit Inside Another App?在另一个应用程序中安装 Django Taggit?
【发布时间】:2021-12-03 20:18:03
【问题描述】:

我正在使用Django taggit。最初,我创建了一个tags 应用程序并稍微定制了taggit。这不是很多代码,也不能保证它是自己的应用程序(在我看来),所以我想将tags 代码移动到另一个应用程序(我们称之为content)。

from content.utils import slugifier

class Tag(TagBase):
    """This is a replacement class for the Taggit "Tag" class"""

    class Meta:
        verbose_name = _("tag")
        verbose_name_plural = _("tags")
        app_label = "tags"

    def slugify(self, tag, i=None):
        slug = slugifier(tag)
        if i is not None:
            slug += "-%d" % i
        return slug

class TaggedItem(GenericTaggedItemBase, TaggedItemBase):
    # This tag field comes from TaggedItemBase - CustomTag overrides Tag
    tag = models.ForeignKey(
        Tag,
        on_delete=models.CASCADE,
        related_name="%(app_label)s_%(class)s_items",
    )

    class Meta:
        verbose_name = _("tagged item")
        verbose_name_plural = _("tagged items")
        app_label = "tags"
        index_together = [["content_type", "object_id"]]
        unique_together = [["content_type", "object_id", "tag"]]

class Content(PolymorphicModel):
    ...
    tags = TaggableManager(through=TaggedItem, blank=True)

我的问题

当我转到makemigrations 时,我收到此错误:

(venv) C:\Users\Jarad\Documents\PyCharm\knowledgetack>python manage.py makemigrations
SystemCheckError: System check identified some issues:

ERRORS:
content.Content.tags: (fields.E300) Field defines a relation with model 'Tag', which is either not installed, or is abstract.

我认为这个错误信息很清楚也很好。我的应用程序content 有一个名为Content 的模型,其中有一个名为tags 的字段。该字段定义了与名为 'Tag'... 的模型的关系,但为什么没有安装它? Tag 模型的代码就在 Content 模型的正上方?

旁注:我的INSTALLED_APPS 中没有列出taggit,因为我正在自定义taggit 并遵循this page 上的建议:

注意:在 settings.py INSTALLED_APPS 列表中包含“taggit”将 创建默认的 django-taggit 和“通过模型”模型。如果你 想使用您自己的模型,您需要删除“taggit” 来自 settings.py 的 INSTALLED_APPS 列表。

我的问题

我需要改变什么?

【问题讨论】:

    标签: django django-models django-taggit


    【解决方案1】:

    我需要更改app_label = "tags"

    您可以删除它或将其更改为要将标签模型移动到的实际应用程序。例如:app_label = "content"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多