Django“查找跨越关系”错误 [英] Django "Lookups that span relationships" Error

查看:1710
本文介绍了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



< >但是,如果我将我的代码更改为不使用用户,而是使用我自己的模型类型栏,那么一切正常工作,我会期望没有错误。示例如下:

  class Foo(models.Model):
initiator = models.ForeignKey(Bar)
date_time = models.DateTimeField()
...

...
now = datetime.now()
bars = Bar.objects.filter(foo__date_time__gte =现在)
...

任何人都可以向我解释第一个代码的问题使用用户模型作为外键?感谢提前!



编辑:我应该澄清一点,我的查询代码不在视图函数中,而是在使用manage.py命令调用的实用程序函数中。如果我将查询代码放在视图中,那么一切都可以正常运行,没有错误!但是好奇的是,第二个代码示例在视图和管理命令场景中都可以正常工作。



希望有一个Django的专长比我更有可能解释这个。谢谢!

解决方案

好的,问题解决了!这似乎是引起这个问题的令人难以置信的微小细节,我不能说我完全明白为什么它表现得像这样,但是这里:



我有导入我的models.py顶部的django.contrib.auth.admin import UserAdmin 以及其他导入语句的。这实际上是在将我的管理员重构到自己的admin.py之前遗留下来的,所以在models.py中没有使用UserAdmin导入。我评论了这个未使用的语句,然后做了一个syncdb,并得到一个反向查询名称冲突的模型验证错误(这是在我的用户配置文件模型中的ForeignKey字段,但不是在我的原始问题无法查询的模型)。所以我在该字段中添加了一个related_name参数,做了一个syncdb,执行了以前提供错误的原始查询,一切都无误!



它从django.contrib.auth.admin导入UserAdmin 中的错误



感谢所有回复试图帮助!


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!

解决方案

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!

这篇关于Django“查找跨越关系”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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