Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between QuerySet , Tuple , Dictionary in Django template

I am having trouble in understanding how to iterate over QuerySet , Tuple and Dictionarty in django.

I am confused which djnago functions returns what like objects.all or objects.get

Suppose i have

a = model.objects.all()
b = model.object.get(pk=1)
c = Blog.objects.values('name', 'entry__headline')
d = Entry.objects.values_list('id', 'headline')
e = Person.objects.raw('SELECT * FROM myapp_person')

What is the retured in each scenario and the biggest problem is how can i iterate over. All these confuse me very much . I studies the docs but they tell one thing and don't tell how to use in template. I know its related to python but then python don't have template to deal with

like image 460
tej.tan Avatar asked Jul 07 '11 14:07

tej.tan


2 Answers

QuerySet: A Django class that processes SQL responses and returns a python construct representing the results. Although it functions like a list in many ways, it's actually what's called an "iterable". It simply mocks the behavior of a list to allow you to use things like for-loops on it.

Tuple: An immutable list. That means that once it's set, it can't be altered. In virtually every other way it behaves just like a list.

Dictionary: Also known as a hash in other languages. It can be considered a "keyed list". A "list" in the strictest of senses is a group of items stored serially in memory. In the old days of programming, you'd have to "pop" items off and "push" items onto a list, and they could only be retrieved in a FIFO, or first-in-first-out fashion. Dictionaries provide a way to "lookup" items in a list. It is composed of key-value pairs, so you can reference a key and get the attached value.

Now in terms of Django templates:

QuerySets: You iterate over these using the standard methods. Once you get a result from something like MyModel.objects.all(), you can use a {% for value in queryset %} tag.

Tuples: As far as iteration goes, these behave exactly as standard lists. You can also just simply use a {% for value in tuple %} tag. The only potential hangup is that sometimes you'll end up with tuples of tuples or a list of tuples. These are just like multi-level lists. You have to iterate over the outer list or tuple and then iterate over the inner ones.

Dictionaries: These are probably the most complicated, only because they require a method call to get an iterable object.

mydict.iteritems() # returns an iterable consisting of `(key, value)` tuples.

mydict.iterkeys() # returns an iterable consisting of the keys. You can then use mydict[key] to get the values

mydict.itervalues() # returns an iterable consisting of the values.

The last method is probably the best of simple iteration in a Django template:

{% for value in mydict.itervalues %}
like image 188
Chris Pratt Avatar answered Oct 22 '22 02:10

Chris Pratt


Are you referring to these docs? https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for

I think that's what you were looking for.

Basically you iterate over them like:

{% for item in a %}
{{item.field}}
{{item.field2}}
{% endfor %}

{{b.field}}

{% for item in c %}
{{item.name}}
{{item.entry__headline}}
{% endfor %}

{% for item in d %}
{{item}}
{% endfor %}

# Also you can do this if you want to access just a particular index:
{{d.0}}
{{d.1}}

{% for item in e %}
{{item.field}}
{{item.field2}}
{% endfor %}

As for your data types:
a would be a QuerySet or list of model objects
b would be a model object
c would be a ValuesQuerySet or a list of dictionaries
d would also be a ValuesQuerySet but it's actually a list of tuples
e would be a RawQuerySet, which acts like a normal QuerySet

Sources:
https://docs.djangoproject.com/en/dev/topics/db/sql/#django.db.models.Manager.raw
https://docs.djangoproject.com/en/dev/ref/models/querysets/#values
https://docs.djangoproject.com/en/dev/ref/models/querysets/#values-list

like image 41
Bryce Siedschlaw Avatar answered Oct 22 '22 00:10

Bryce Siedschlaw