Django“跨越关系的查找”错误
我正在尝试遵循跨越“反向”关系的关系的查找文档,发现 此处。这是我的模型代码:
class Foo(models.Model):
initiator = models.ForeignKey(User)
date_time = models.DateTimeField()
...
这是我的查询代码:
...
now = datetime.now()
users = User.objects.filter(foo__date_time__gte = now)
...
这会导致以下错误:django.core.exceptions.FieldError:无法将关键字“foo”解析为字段。选项包括:_message_set、date_joined、email、first_name、groups、id、is_active、is_staff、is_superuser、last_login、last_name、logentry、password、user_permissions、username
但是,如果我将代码更改为不使用 User,而是使用使用我自己的模型类型 Bar,那么一切都会按我的预期进行,没有错误。下面的示例:
class Foo(models.Model):
initiator = models.ForeignKey(Bar)
date_time = models.DateTimeField()
...
...
now = datetime.now()
bars = Bar.objects.filter(foo__date_time__gte = now)
...
任何人都可以向我解释使用 User 模型作为外键的第一个代码的问题吗?提前致谢!
编辑:我应该澄清一下,我的查询代码不在视图函数中,而是在我使用 manage.py 命令调用的实用程序函数中。如果我将查询代码放入视图中,那么一切都会正常工作,没有错误!但奇怪的是,第二个代码示例在视图和管理命令场景中都运行良好。
希望比我更有 Django 专业知识的人可以解释这一点。谢谢!
I'm trying to follow the documentation for lookups that span relationships for a "reverse" relationship, found here. Here is my model code:
class Foo(models.Model):
initiator = models.ForeignKey(User)
date_time = models.DateTimeField()
...
And here is my query code:
...
now = datetime.now()
users = User.objects.filter(foo__date_time__gte = now)
...
This results in the following error: django.core.exceptions.FieldError: Cannot resolve keyword 'foo' into field. Choices are: _message_set, date_joined, email, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, logentry, password, user_permissions, username
However, if I change my code to not use User, but instead use my own model type Bar, then everything works as I would expect with no errors. Example below:
class Foo(models.Model):
initiator = models.ForeignKey(Bar)
date_time = models.DateTimeField()
...
...
now = datetime.now()
bars = Bar.objects.filter(foo__date_time__gte = now)
...
Can anyone explain to me the problem with the first code that uses the User model as the foreign key? Thanks in advance!
EDIT: I should clarify that my query code is not in a view function, but in a utility function that I call using a manage.py command. If I put the query code in a view then everything works fine with no errors! But the curious thing is that the second code example works fine in both the view and the management command scenarios.
Hopefully someone with a little more Django expertise than me can explain this. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,问题解决了!这似乎是导致此问题的一个令人难以置信的微小细节,我不能说我完全理解为什么它会这样表现,但这里是:
我有
from django.contrib.auth.admin import UserAdmin 位于 models.py 的顶部,以及其他导入语句。这实际上是我将管理内容重构到自己的 admin.py 之前留下的,因此 UserAdmin 导入根本没有在 models.py 中使用。我注释掉了这个未使用的语句,然后执行了syncdb,并在冲突的反向查询名称上收到了模型验证错误(这是在我的用户配置文件模型中的ForeignKey字段中,但不是我在原始问题中无法查询的模型) )。因此,我向该字段添加了 related_name 参数,执行了syncdb,执行了之前给出错误的原始查询,并且一切都没有错误!
所以最终它归结为错误的
from django.contrib.auth.admin import UserAdmin
。感谢所有回复并试图提供帮助的人!
Ok, problem solved! This seems like an incredibly minute detail that was causing this issue, and I can't say I fully understand why it manifested like it did, but here goes:
I had
from django.contrib.auth.admin import UserAdmin
at the top of my models.py, along with my other import statements. This was actually left over from before I refactored out my admin stuff into its own admin.py, so the UserAdmin import was not being used at all in models.py. I commented out this unused statement, then did a syncdb and got a model validation error on a reverse query name that clashed (this was in a ForeignKey field in my User Profile model, but not the model I couldn't query in my original question). So I added a related_name argument to that field, did a syncdb, executed my original query that was giving errors previously, and everything worked error-free!So in the end it boiled down to an errant
from django.contrib.auth.admin import UserAdmin
.Thank you to all who responded trying to help!