Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 第二个应用程序中的Django ManyToManyField错误-无法解析关键字';xxxx进入现场_Python_Database_Django - Fatal编程技术网

Python 第二个应用程序中的Django ManyToManyField错误-无法解析关键字';xxxx进入现场

Python 第二个应用程序中的Django ManyToManyField错误-无法解析关键字';xxxx进入现场,python,database,django,Python,Database,Django,我正在尝试编写一个用户可以提交照片的应用程序。其中一部分涉及到为照片投票。因此,在我的models.py中,我有: photos/models.py class Photo(models.Model): votes = models.IntegerField(default=0) users_voted = models.ManyToManyField(User, related_name='voters', blank=True, null=True) class MyMPT

我正在尝试编写一个用户可以提交照片的应用程序。其中一部分涉及到为照片投票。因此,在我的models.py中,我有:

photos/models.py

class Photo(models.Model):
    votes = models.IntegerField(default=0)
    users_voted = models.ManyToManyField(User, related_name='voters', blank=True, null=True)
class MyMPTTComment(MPTTModel, Comment):
        parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
        cvotes = models.IntegerField(default=0)
        users_voted = models.ManyToManyField(User, related_name='cvoters')
用户在ManyToManyField投了很多票,这确保了人们不能再投两次票。这一切都很好,但目前我正在开发项目中的另一个应用程序,它不提供MPTT评论。我希望用户也能投票支持彼此的评论。因此,我将投票代码复制到我的评论模型中:

comments/models.py

class Photo(models.Model):
    votes = models.IntegerField(default=0)
    users_voted = models.ManyToManyField(User, related_name='voters', blank=True, null=True)
class MyMPTTComment(MPTTModel, Comment):
        parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
        cvotes = models.IntegerField(default=0)
        users_voted = models.ManyToManyField(User, related_name='cvoters')
这将按预期在数据库中创建一个表。但当我尝试使用view.py代码访问它时,我会得到FieldError:

  def comment_vote(request):
    if request.GET.has_key('id'):
        try:
            id = request.GET['id']
            target = MyMPTTComment.objects.get(id=id)
            user_voted = target.users_voted.filter(
            username=request.user.username
            )
我已经为这件事挠头好一阵子了。其他有类似问题的人发现,这可能与Django加载模块()的时间有关,但摆弄模块命令来影响它们的加载时间没有任何作用

错误显示
选项为:。。。选民
,那么为什么不能解决投票人

编辑:添加完整错误和回溯:

Internal Server Error: /commentvote/
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py", line 25, in _wrapped_view
return view_func(request, *args, **kwargs)
  File "/root/photoproject/photos/views.py", line 300, in comment_vote
username=request.user.username
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 155, in filter
return self.get_query_set().filter(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 615, in get_query_set
return super(ManyRelatedManager, self).get_query_set().using(db)._next_is_sticky().filter(**self.core_filters)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 669, in filter
return self._filter_or_exclude(False, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 687, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1271, in add_q
can_reuse=used_aliases, force_having=force_having)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1139, in add_filter
process_extras=process_extras)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1337, in setup_joins
"Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'cvoters' into field. Choices are: comment_comments, comment_flags, date_joined, email, first_name, friend_set, groups, id, invitation, is_active, is_staff, is_superuser, last_login, last_name, logentry, moderated_messages, password, photos, received_messages, registrationprofile, sent_messages, to_friend_set, user_permissions, userbio, username, voters
Django v1.5.4

发布数据库架构:

CREATE TABLE "comments_mympttcomment" ("rght" integer unsigned NOT NULL, "parent_id" integer, "level" integer unsigned NOT NULL, "lft" integer unsigned NOT NULL, "tree_id" integer unsigned NOT NULL, "cvotes" integer NOT NULL, "comment_ptr_id" integer PRIMARY KEY);
CREATE TABLE "comments_mympttcomment_users_voted" ("id" integer NOT NULL PRIMARY KEY, "mympttcomment_id" integer NOT NULL, "user_id" integer NOT NULL);
CREATE TABLE "photos_photo" (
"id" integer NOT NULL PRIMARY KEY,
...
"votes" integer NOT NULL,
"user_id" integer REFERENCES "auth_user" ("id"),
);
CREATE TABLE "photos_photo_users_voted" (
"id" integer NOT NULL PRIMARY KEY,
"photo_id" integer NOT NULL,
"user_id" integer NOT NULL REFERENCES "auth_user" ("id"),
UNIQUE ("photo_id", "user_id")
);

刚才注意到comments用户表不包括“REFERENCES”auth_user(“id”)”,但我不知道为什么。如上所述,comments模型包括ManytoManyField,与photos表完全相同,从django.contrib.auth.models导入用户导入。

我认为问题是由于在
MyMPTTComment
中使用多重继承造成的。从模型定义中删除注释:

class MyMPTTComment(MPTTModel):
        parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
        cvotes = models.IntegerField(default=0)
        users_voted = models.ManyToManyField(User, related_name='cvoters')

您可以再次添加父字段。我发现了一个与您想要执行的类似的实现。

您试图在queryset管理器上进行筛选,而不是在字段上进行筛选。当你说:

users_voted = models.ManyToManyField(User, related_name='cvoters')
您要求Django向用户实例添加名为
cvoters
的管理器,而不是向用户模型添加新字段

我引述


因此,由于cvoters不是一个字段,因此无法对其进行过滤。而是根据您感兴趣的实际字段进行筛选。

请发布准确的错误…如果发布完整的错误和回溯,则很好。您使用的是什么Django版本?版本1.5。对不起,我的问题中没有包括所有这些重要信息;我忘记了。来自
Photo
的用户投票字段有两个额外参数,blank=True,null=True。
MyMPTTComment
中的用户投票字段没有这些内容。也许这就是为什么它只显示
选民
,而不显示
投票人
。我不认为是这样。我已经尝试过只从注释继承,没有MPTT继承,但仍然遇到问题。加上那个教程似乎不完整。嗨,雷德尔。我已经试着改名了,但还是有问题。另外,它没有解释为什么它适用于照片模型,照片模型以相同的方式使用相关的名称。这不是名称问题。您是否在照片模型中应用了一些涉及投票人的过滤器?如果不是这样,照片模型就是这样工作的。