Django:无法将关键字“”解析为字段。选择是: [英] Django: Cannot resolve keyword '' into field. Choices are:

查看:491
本文介绍了Django:无法将关键字“”解析为字段。选择是:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在访问 ManyToManyField 时遇到了这个奇怪的问题。

I am having this weird problem accessing ManyToManyField.

我有以下模型。

class Link(models.Model):
    title = models.CharField(max_length = 200)
    url = models.URLField(unique = True)
    tags = models.ManyToManyField(Tag)
    creation_date = models.DateTimeField(auto_now_add = True)
    user = models.ForeignKey(User)
    likes = models.ManyToManyField(User, related_name = "%(app_label)s_%(class)s_user_likes")
    dis_likes = models.ManyToManyField(User, related_name = "%(app_label)s_%(class)s_user_dis_likes")

    class Meta:
        abstract = True

class URL(Link):
    preview_image = models.URLField()
    preview_heading = models.CharField(max_length = 100)
    preview_content = models.CharField(max_length = 100)

当我尝试时访问 URL.objects.get(pk = 1).likes.all(),我得到无法重新访问将关键字’解决到字段中。选择是...... 错误。

When I try to access URL.objects.get(pk=1).likes.all(), I get Cannot resolve keyword '' into field. Choices are:... error.

URL.objects.get(pk = 1).tags.all (),URL.objects.get(pk = 1).user URL.objects.filter(likes = auser,pk = 1)

更新:


  1. 字段使用 south 通过 schemamigration 和 dis_likes

  2. 以前我使用的是 Django 1.6.1 ,更新为 Django 1.6.2 ,问题仍然存在

  3. 截断数据库,将其同步为具有新表,问题仍然存在

  4. 部分回溯:

  1. The fields likes and dis_likes were added using south through schemamigration
  2. Previously I was using Django 1.6.1, updated to Django 1.6.2, the problem still persists
  3. Truncated the database, synced it to have fresh tables, the problem still persists
  4. Partial traceback:

File "F:\system\env\lib\site-packages\django\db\models\manager.py" in all
  133.         return self.get_queryset()
File "F:\system\env\lib\site-packages\django\db\models\fields\related.py" in get_queryset
  549.                 return super(ManyRelatedManager, self).get_queryset().using(db)._next_is_sticky().filter(**self.core_filters)
File "F:\system\env\lib\site-packages\django\db\models\query.py" in filter
  590.         return self._filter_or_exclude(False, *args, **kwargs)
File "F:\system\env\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  608.             clone.query.add_q(Q(*args, **kwargs))
File "F:\system\env\lib\site-packages\django\db\models\sql\query.py" in add_q
  1198.         clause = self._add_q(where_part, used_aliases)
File "F:\system\env\lib\site-packages\django\db\models\sql\query.py" in _add_q
  1234.                     current_negated=current_negated)
File "F:\system\env\lib\site-packages\django\db\models\sql\query.py" in build_filter
  1100.                     allow_explicit_fk=True)
File "F:\system\env\lib\site-packages\django\db\models\sql\query.py" in setup_joins
  1357.             names, opts, allow_many, allow_explicit_fk)
File "F:\system\env\lib\site-packages\django\db\models\sql\query.py" in names_to_path
  1277.                                      "Choices are: %s" % (name, ", ".join(available)))

Exception Type: FieldError at /url/3
Exception Value: Cannot resolve keyword '' into field. Choices are: __app___article_user_dis_likes, __app___article_user_likes, __app___imageurl_user_dis_likes, __app___imageurl_user_likes, __app___review_user_dis_likes, __app___review_user_likes, __app___url_user_dis_likes, __app___url_user_likes, __app___videourl_user_dis_likes, __app___videourl_user_likes, article, date_joined, email, first_name, groups, id, imageurl, is_active, is_staff, is_superuser, last_login, last_name, logentry, password, review, url, user_permissions, username, userobjectpermission, videourl



推荐答案

我认为我已经找到了问题。我想问题出在我的应用名称是 __ app __ 。 Django字段查找假设 __ (双下划线)之前的所有内容都是一个字段,在我看来,该字段解析为``(空字符串)。

I think I have found the problem. I suppose the problem is with the name of my app which is __app__. Django field look up assumes everything before __(double underscore) is a field which in my case resolves to ``(empty string).

总是很难命名默认的应用程序及其所在的项目。以为 __ app __ 是更Python且更聪明的解决方案。我想我应该将我的应用重命名为 app 。希望这行得通。

Always had hard time naming the default app and the project it lives in. Thought __app__ was more pythonic and clever solution. I guess I should rename my app to just app. Hope this works.

这篇关于Django:无法将关键字“”解析为字段。选择是:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆