I have been able to get my database queries to work properly for deleting existing entries and also adding new entries to the database but I am completely stumped as to why I am unable to retrieve anything from my database. I am trying a query such as:
我已经能够使我的数据库查询正常工作,以便删除现有的条目,并向数据库添加新条目,但是我完全搞不懂为什么我不能从数据库中检索任何内容。我正在尝试这样的查询:
from web1.polls.models import Poll
retquery = Poll.objects.all()
print retquery
--prints: "[ ]"
Also, if I try this, it just returns "poll object"
同样,如果我尝试这个,它只返回"poll对象"
from web1.polls.models import Poll
retquery = Poll.objects.all()[0]
print retquery
--prints: "poll object"
I have looked at everything and there are definitely entries in the database, I have tried this with a number of different models where everything else is working otherwise so I don't know what I can do at this point, any advice is greatly appreciated
我看了所有的东西,在数据库中肯定有条目,我尝试了很多不同的模型,其他的东西都在工作,所以我不知道在这一点上我能做什么,任何建议都非常感谢。
2 个解决方案
#1
1
if you have provide well __unicode__
method to your model, you won't have such problem...
如果您已经为您的模型提供了良好的__unicode__方法,您就不会有这样的问题……
http://www.djangoproject.com/documentation/models/str/
#2
0
I finally figured this out, I am leaving this up since this can be really confusing for someone fairly new to Python, such as myself, as Django's docs don't make this entirely clear, I'm used to printing out an object and having it list everything in it but for some reason the type of object that is returned is not in such a format that doing this will work, so you need to take the resulting variable from the query and add the name of the field to the end in order to access it, such as:
我终于知道了,我离开这个因为这可以真正令人困惑的人相当新的Python,比如我自己,Django的文档不完全清楚,我用来打印出一个对象,它列出的一切,但出于某种原因返回的类型的对象不是在这样的格式,这样做会工作,所以你需要从查询和结果变量的名称字段添加到最后为了访问它,如:
If I have a field named "question" that I want to retrieve:
retquery = Poll.objects.all()
print retquery.question
This will work, whereas the way I was doing it before it just printed nothing making me think that the returned object was empty
这是可行的,而我之前的做法是什么都不打印,这让我觉得返回的对象是空的
#1
1
if you have provide well __unicode__
method to your model, you won't have such problem...
如果您已经为您的模型提供了良好的__unicode__方法,您就不会有这样的问题……
http://www.djangoproject.com/documentation/models/str/
#2
0
I finally figured this out, I am leaving this up since this can be really confusing for someone fairly new to Python, such as myself, as Django's docs don't make this entirely clear, I'm used to printing out an object and having it list everything in it but for some reason the type of object that is returned is not in such a format that doing this will work, so you need to take the resulting variable from the query and add the name of the field to the end in order to access it, such as:
我终于知道了,我离开这个因为这可以真正令人困惑的人相当新的Python,比如我自己,Django的文档不完全清楚,我用来打印出一个对象,它列出的一切,但出于某种原因返回的类型的对象不是在这样的格式,这样做会工作,所以你需要从查询和结果变量的名称字段添加到最后为了访问它,如:
If I have a field named "question" that I want to retrieve:
retquery = Poll.objects.all()
print retquery.question
This will work, whereas the way I was doing it before it just printed nothing making me think that the returned object was empty
这是可行的,而我之前的做法是什么都不打印,这让我觉得返回的对象是空的