djangodjango-modelsdjango-taggit

Install Django Taggit Inside Another App?


I am using Django taggit. Initially, I created a tags app and customized taggit slightly. It's not a lot of code and doesn't warrant it's own app (in my opinion) so I want to move the tags code to another app (let's call it 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)

My Problem

When I go to makemigrations, I get this error:

(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.

I think this error message is pretty clear and good. My app content has a model named Content which has a field named tags. The field defines a relationship with a model named 'Tag'... but why isn't it installed? The code for the Tag model is literally right above the Content model?

Sidenote: I don't have taggit listed in my INSTALLED_APPS because I am customizing taggit and following the advice on 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.

My Question

What do I need to change?


Solution

  • I needed to change the app_label = "tags".

    You can remove it or change it to the actual app that you are moving the tags models to. Ex: app_label = "content"