Iterating over a QuerySet

2010-01-20 Thread Olivier Guilyardi
For best performances, should I: records = MyModel.objects.all() for item in records: ... or is the following ok: for item in MyModel.objects.all(): ... ? -- Olivier -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this

iterating over a queryset?

2013-09-17 Thread Johan Bergkvist
Hi I'm quite new to both django and python, and OOP for that matter. I do have some embedded programming experience though. I have a question regarding querysets and dictionaries, I think. I was trying to build a model that provides an overview for subscribed customers over their services and s

iterating over a queryset?

2013-09-17 Thread Daniel Roseman
Without the get(), you get a queryset, which is an iterable of all elements that match the query - in effect, a list of dicts. Even though only one record matches, you still get a queryset containing a single dict. So in order to use that in your template, you'd have to add an outer loop iterati

Re: Iterating over a QuerySet

2010-01-20 Thread Daniel Roseman
On Jan 20, 7:09 pm, Olivier Guilyardi wrote: > For best performances, should I: > > records = MyModel.objects.all() > for item in records: >     ... > > or is the following ok: > > for item in MyModel.objects.all(): >     ... > > ? There is no difference, except for the infinitesimal amount of ti

Re: Iterating over a QuerySet

2010-01-20 Thread Olivier Guilyardi
On 01/20/2010 08:41 PM, Daniel Roseman wrote: > On Jan 20, 7:09 pm, Olivier Guilyardi wrote: >> For best performances, should I: >> >> records = MyModel.objects.all() >> for item in records: >> ... >> >> or is the following ok: >> >> for item in MyModel.objects.all(): >> ... >> >> ? > > T

Re: iterating over a queryset?

2013-09-18 Thread Johan Bergkvist
Hi, thanks - that provides some perspective. So if I omitted the .get() and .values() i will have to loop over the queryset and then each dict, doing something like: {% for q in info %} {% for k, v in q.items %} {{ k }} {{ v}} {% empty %}

Re: iterating over a queryset?

2013-09-18 Thread Bill Freeman
No. q will be a model instance, not a dict. Your inner loop would, perhaps, loop over a sequence of names (like the arguments you were passing to value()) and use those names to access the attributes of q. This might more cleanly handled with a method on the q object's class that returns the des

Re: iterating over a queryset?

2013-09-18 Thread Daniel Roseman
Well, not quite. That will work if you just omit .get(), because then you'll have a ValuesQuerySet - basically, a list of dicts. However, if you omit .values() as well you'll have a plain old QuerySet - ie a list of model instances - and those instances aren't dicts and don't have an .items() m

Re: iterating over a queryset?

2013-09-18 Thread Johan Bergkvist
Hi Yeah I see. I tried it out just after writing, with little success, but without the .values() I do still have the {{ data.field_name }} available which Is probably more usefull than actually iterating over a dataset regardless of content. Still the relationsships are only available as forward

Re: iterating over a queryset?

2013-09-18 Thread Bill Freeman
.get() actually gets you an instance, while the queryset without the get acts (somewhat) like a list of instances. It is the .values() call that causes you to have dicts rather than instances. .get() considers it an error if the queryset represents either zero objects, or two or more objects (it

Re: iterating over a queryset?

2013-09-21 Thread Johan Bergkvist
Daniel Roseman adviced me to use the Shell. This was a fantastic idea, and one I should have thought of myself! Anyway this helped me realize that I was referencing related models the wrong way. For some reason I thought that you should reference the model name and not the foreign key field nam

Re: iterating over a queryset?

2013-09-22 Thread kooliah
Sorry...I wanted open a new discussion but i reply instead... -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post

Iterating over a queryset to run another query?

2010-10-13 Thread Austin Govella
I have a category page that lists all of its subcategories on it. For each subcategory, I would like to query for one product in that subcategory. In PHP, I think I did this in the page, outputting HTML as I went. But I can't figure out how to do this in Django. My programming brain is just rusty

Re: Iterating over a queryset to run another query?

2010-10-13 Thread John M
is the subcategory the same for all the top level categories? if so, i think you should be able to query your category queryset, like this? finalset = Category.objects.filter().filter(subcategory__field=) Unless I'm not understanding. On Oct 13, 10:52 am, Austin Govella wrote: > I h