Django objects.all()空查询集,在外壳中不为空 [英] Django objects.all() empty query set, not empty in shell

查看:332
本文介绍了Django objects.all()空查询集,在外壳中不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去两年来我一直在使用Django,一点问题都没有,我不是菜鸟.

I have been using Django for the past 2 years with no issues at all, I am no rookie.

我最近为模型创建了一个新属性,并将其添加到mysql数据库中.

I recently created a new attribute for a model and added it to the mysql database.

每当我在"views.py"中使用此属性过滤内容时,我都会得到一个空集,每当我使用"manage.py shell"使用相同的确切代码行时,它就会返回预期的查询集.

Whenever I filter things using this attribute in my "views.py" i get an empty set, whenever I use the same exact line of code using "manage.py shell" it returns the expected queryset.

我尝试重新启动httpd服务,mysqld服务,并且重新启动了系统,但是什么也没发生.

I have tried restarting the httpd service, the mysqld service and I have rebooted the system but nothing happens.

有人知道如何绕过这个问题吗?

Does anyone know how to bypass this issue?

这是我用来调试此问题的代码:

Here's the code I used to debug this issue:

外壳: 它不会让我发布图片,因此代码如下:

Shell: It won't let me post pictures so here's the code:

>>>Session.objects.filter(cancellation_session=True)
>>>[<Session: Zumba Toning (Ladies)>]

views.py:

def gym_day2(request):
    return HttpResponse(str(Session.objects.filter(cancellation_session=True)))

HttpResponse:

HttpResponse:

[]

请帮助!

推荐答案

add.语句之后的value或.values_list()Session.objects.filter(cancellation_session = True).values(´field´)

add . values or .values_list() after the statement , Session.objects.filter(cancellation_session=True).values(´field´)

这篇关于Django objects.all()空查询集,在外壳中不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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