Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.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 x、 objects.all()为空,user.x_set.all()不为空_Python_Django - Fatal编程技术网

Python x、 objects.all()为空,user.x_set.all()不为空

Python x、 objects.all()为空,user.x_set.all()不为空,python,django,Python,Django,这可能真的很简单,但我想我以前没有遇到过这个问题 我的数据库中有一个名为WeatherForecast的对象,这个对象对用户有一个外键,因此用户可以在模板中显示许多不同的预测 class WeatherForecast(models.Model): owner = models.ForeignKey(User, null=True) 我创建预测并将外键设置为: newForecast = WeatherForecast.objects.create(owner = request.

这可能真的很简单,但我想我以前没有遇到过这个问题

我的数据库中有一个名为WeatherForecast的对象,这个对象对用户有一个外键,因此用户可以在模板中显示许多不同的预测

class WeatherForecast(models.Model):


    owner = models.ForeignKey(User, null=True)
我创建预测并将外键设置为:

newForecast = WeatherForecast.objects.create(owner = request.user)
当我调用
WeatherForecast.objects.all()
时,它返回一个空查询集。但是,当我调用
user.weatherforecast\u set.all()
时,它会返回带有正确外键的weatherforecast

我可能遗漏了一些明显的东西,但为什么会发生这种情况

编辑:

这就是我所说的

最初,我这样做是为了获得一些选定的预测(通过表单从模板中进行选择,ID从表单中获取并放入列表,我已经验证了ID是否正确地从表单中传递)

我得到了与此查询匹配的
天气预报,该查询不存在错误。查看之后,我发现了我遇到的问题,其中
objects.all()
为空,而
weatherforecast\u set.all()
为空

为了确保我没有遗漏一些愚蠢的东西,我还打印了如下查询集:

print "all weather forecasts:", WeatherForecast.objects.all()
print "user weather forecasts:", request.user.weatherforecast_set.all()
其中:

all weather forecasts: <QuerySet []>
user weather forecasts: <QuerySet [<WeatherForecast: WeatherForecast object>]>
所有天气预报:
用户天气预报:
编辑2

修正!另一个应用程序中有一个同名的模型,导致查询在错误的表中查找!现在觉得自己有点像个白痴


谢谢大家的帮助

你说的是不可能的。您可能正在做一些您没有在此处显示的其他事情。您是否在天气预报上更改了model manager?谢谢您的回复!不,我没有更改任何与模型管理器有关的内容。您可以在调用代码的位置显示代码吗?1。您能否显示两个调用的确切输出,即WeatherForecast.objects.all()和user.WeatherForecast\u set.all()2。您是否也可以转到数据库并从{{app_name}点击sql“选择计数(*)_weatherforecast并在此处向我们展示?您所说的是不可能的。您可能正在做一些您在此处未向我们展示的其他事情。您是否在weatherforecast上更改了model manager?谢谢您回复我!不,我没有更改任何与model manager有关的内容。您可以在您称之为代码的位置展示代码吗?1.您能否显示n两个调用,即WeatherForecast.objects.all()和user.WeatherForecast\u set.all()2.您是否也可以转到数据库并从{{app\u name}}}WeatherForecast中点击sql“select count(*)并在此处显示?
all weather forecasts: <QuerySet []>
user weather forecasts: <QuerySet [<WeatherForecast: WeatherForecast object>]>