python – Django:无法将关键字”解析为字段.选择是:

前端之家收集整理的这篇文章主要介绍了python – Django:无法将关键字”解析为字段.选择是:前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有这个访问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()时,我得到无法将关键字”解析为字段.选择是:…错误.

URL.objects.get(pk = 1).tags.all(),URL.objects.get(pk = 1).user和URL.objects.filter(likes = auser,pk = 1)工作正常.

更新:

>通过schemamigration使用south添加了字段like和dis_likes
>以前我使用Django 1.6.1,更新到Django 1.6.2,问题仍然存在
>截断数据库,同步它以获得新表,问题仍然存在
>部分追溯:

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字段查找假设__(双下划线)之前的所有内容都是一个字段,在我的情况下解析为“(空字符串).

总是很难命名默认应用程序及其所在的项目.思想__app__是更加pythonic和聪明的解决方案.我想我应该将我的应用程序重命名为app.希望这有效.

原文链接:https://www.f2er.com/python/439736.html

猜你在找的Python相关文章