[英]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.