在另一个应用程序中安装 Django Taggit?

Install Django Taggit Inside Another App?

我正在使用 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:

上的建议

Note: Including ‘taggit’ in settings.py INSTALLED_APPS list will create the default django-taggit and “through model” models. If you would like to use your own models, you will need to remove ‘taggit’ from settings.py’s INSTALLED_APPS list.

我的问题

我需要更改什么?

我需要更改 app_label = "tags"

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