尝试在我的应用中使用标记包时遇到错误。
Cannot resolve keyword 'items' into field. Choices are: id, name
这是来自这行代码:
current_tags = list(self.filter(items__content_type__pk=ctype.pk,
items__object_id=obj.pk))
以下是完整的追溯:
Request Method: POST
Request URL: http://localhost:8000/admin/coltrane/entry/add/
Django Version: 1.7
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'search',
'coltrane',
'markdown')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
567. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
105. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
204. return view(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in add_view
1437. return self.changeform_view(request, None, form_url, extra_context)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
105. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
25. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/Library/Python/2.7/site-packages/django/db/transaction.py" in inner
394. return func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in changeform_view
1388. self.save_model(request, new_object, form, not add)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in save_model
1029. obj.save()
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save
590. force_update=force_update, update_fields=update_fields)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save_base
627. update_fields=update_fields, raw=raw, using=using)
File "/Library/Python/2.7/site-packages/django/dispatch/dispatcher.py" in send
198. response = receiver(signal=self, sender=sender, **named)
File "/Users/byrd/Virtual_env_new/lib/python2.7/site-packages/tagging/fields.py" in _save
81. Tag.objects.update_tags(kwargs['instance'], tags)
File "/Users/byrd/Virtual_env_new/lib/python2.7/site-packages/tagging/models.py" in update_tags
34. items__object_id=obj.pk))
File "/Library/Python/2.7/site-packages/django/db/models/manager.py" in manager_method
92. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in filter
691. return self._filter_or_exclude(False, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
709. clone.query.add_q(Q(*args, **kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in add_q
1287. clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in _add_q
1314. current_negated=current_negated, connector=connector)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in build_filter
1138. lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in solve_lookup_type
1076. _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in names_to_path
1383. self.raise_field_error(opts, name)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in raise_field_error
1389. "Choices are: %s" % (name, ", ".join(available)))
Exception Type: FieldError at /admin/coltrane/entry/add/
Exception Value: Cannot resolve keyword 'items' into field. Choices are: id, name
以下是标记“items is related_name”
的模型@python_2_unicode_compatible
class Tag(models.Model):
"""
A tag.
"""
name = models.CharField(_('name'), max_length=50, unique=True, db_index=True)
objects = TagManager()
class Meta:
ordering = ('name',)
verbose_name = _('tag')
verbose_name_plural = _('tags')
def __str__(self):
return self.name
@python_2_unicode_compatible
class TaggedItem(models.Model):
"""
Holds the relationship between a tag and the item being tagged.
"""
tag = models.ForeignKey(Tag, verbose_name=_('tag'), related_name='items')
content_type = models.ForeignKey(ContentType, verbose_name=_('content type'))
object_id = models.PositiveIntegerField(_('object id'), db_index=True)
object = generic.GenericForeignKey('content_type', 'object_id')
objects = TaggedItemManager()
class Meta:
# Enforce unique tag association per object
unique_together = (('tag', 'content_type', 'object_id'),)
verbose_name = _('tagged item')
verbose_name_plural = _('tagged items')
def __str__(self):
return '%s [%s]' % (smart_text(self.object), smart_text(self.tag))
答案 0 :(得分:1)
之前我遇到过这个问题。
这是因为我忘记了包含标记' app进入' settings.py'的INSTALLED_APP元组。对于我的网站,除了安装'标记'机器上的应用程序。
希望有所帮助
答案 1 :(得分:0)
要让Tag()
获取TaggedItem()
个对象上特定Tag()
为ForeignKey
的所有TaggedItem()
个对象,您可能希望将其作为tag = Tag.objects.get(pk=1) # for example
tag.taggeditem_set() # returns all related taggeditems
:
{{1}}
以下是相关的Django文档: