问题描述
我正在尝试创建一种具有与公司和主题相关的两种类型的标签的模型,并按照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,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()])