[英]django-taggit Error on calling names() on Custom Tag
我正在尝试创建一个模型,其中包含与公司和主题相关联的两种类型的标签,并遵循 django-taggit 中的文档制作自定义标签,以便在一个模型中拥有两个 Taggable Manager。
我的模型.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")
现在,当我尝试在 django shell 中运行以下命令时
from QuestionBank.models import Questions
Questions.objects.first().company_tags.names()
我收到以下错误:
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
提前致谢!
我认为下面的代码可以解决您的问题:
[t.name for t in your_tags.all()]
我在我的models.py
定义了这个,下面的例子可能有帮助:
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()])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.