简体   繁体   中英

django-taggit Error on calling names() on Custom Tag

I am trying to create a model with two types of tags associated with companies and topics and followed the documentation at django-taggit for making Custom Tags to facilitate having two Taggable Manager in one model.

My models.py

from django.db import models
from taggit.managers import TaggableManager
from taggit.models import GenericTaggedItemBase, TagBase

# Create your models here.
class TopicTag(TagBase):
    class Meta:
        verbose_name = "TopicTag"
        verbose_name_plural = "TopicTags"

class CompanyTag(TagBase):
    class Meta:
        verbose_name = "CompanyTag"
        verbose_name_plural = "CompanyTags"

class ThroughTopicTag(GenericTaggedItemBase):
    tag = models.ForeignKey(TopicTag, on_delete=models.CASCADE)

class ThroughCompanyTag(GenericTaggedItemBase):
    tag = models.ForeignKey(CompanyTag, on_delete= models.CASCADE)


class Questions(models.Model):
    name = models.CharField(max_length=255)
    path = models.CharField(max_length=500)
    company_tags = TaggableManager(blank = True, through=ThroughCompanyTag, related_name="company_tags")
    topic_tags = TaggableManager(blank = True, through=ThroughTopicTag, related_name="topic_tags")

Now, when I try to running the following commands in django shell

from QuestionBank.models import Questions
Questions.objects.first().company_tags.names()

I get the following error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/taggit/utils.py", line 124, in inner
    return func(self, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/taggit/managers.py", line 248, in names
    return self.get_queryset().values_list("name", flat=True)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/taggit/managers.py", line 74, in get_queryset
    return self.through.tags_for(self.model, self.instance, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/taggit/models.py", line 155, in tags_for
    return cls.tag_model().objects.filter(**kwargs).distinct()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/query.py", line 904, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/query.py", line 923, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1350, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1377, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1250, in build_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1087, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1482, in names_to_path
    raise FieldError("Cannot resolve keyword '%s' into field. "
django.core.exceptions.FieldError: Cannot resolve keyword 'None' into field. Choices are: company_tags, id, name, slug, throughcompanytag

Thanks in advance!

I think code below can solve your problem:

[t.name for t in your_tags.all()]

I defined this in my models.py , example below may help:

class TagsAdmin(models.Model):
    admin_tags = TaggableManager()
    admin = models.ForeignKey(AdminInit, on_delete=models.CASCADE)

    class Meta:
        verbose_name = _("TagsAdmin")
        ordering = ("created",)

    def __str__(self):
        return '{0}'.format([t.name for t in self.admin_tags.all()])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM