【发布时间】:2013-12-31 01:45:53
【问题描述】:
我正在尝试编写一个用户可以提交照片的应用程序。其中一部分涉及对照片进行投票。所以在我的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)
users_voted ManyToManyField 确保人们不能投票两次。这一切都很好,但目前我正在项目中开发另一个执行 MPTT cmets 的应用程序。我希望用户也能够投票给对方的 cmets。所以我将投票代码复制到我的评论模型中:
comments/models.py
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 加载模块的时间有关 (http://chase-seibert.github.io/blog/2010/04/30/django-manytomany-error-cannot-resolve-keyword-xxx-into-a-field.html),但摆弄模块顺序以试图影响它们的加载时间并没有任何作用。
错误是Choices are: ... voters,那为什么cvoters不能解决呢?
编辑:添加完整的错误和回溯:
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")
);
刚刚注意到 cmets-user 表不包含“REFERENCES "auth_user" ("id")",但我不知道为什么。如上所述,cmets 模型包含的 ManytoManyField 与 photos 表完全一样,从 django.contrib.auth.models import User 导入。
【问题讨论】:
-
请发布确切的错误...
-
如果您发布完整的错误和回溯,那很好。
-
你用的是什么 Django 版本?
-
1.5 版。很抱歉没有在我的问题中包含所有这些重要信息;它让我忘记了。
-
Photo的 users_voted 字段有两个额外的参数,blank=True, null=True。MyMPTTComment的 users_voted 字段没有这些。也许这就是它只显示voters而不显示cvoters的原因。