What is the difference between QuerySet, Tuple, Dictionary in a Django template

I'm having trouble understanding how to iterate through QuerySet, Tuple and Dictionarty in django.

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

Assume that i

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 happens in each scenario, and the biggest problem is how I can iterate over. All this very confuses me. I study documents, but they say one thing and do not say how to use it in a template. I know this is related to python, but then python has no template to work with

+4
source share
2 answers

Do you mean these documents? https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for

I think you were looking.

You basically 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 will be a QuerySet or a list of model objects
b will be a model object
c will be a ValuesQuerySet or a list of dictionaries
d will also be a ValuesQuerySet, but this is actually a list of tuples
e will be a RawQuerySet that acts like a regular 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

+4
source

QuerySet : A Django class that processes SQL responses and returns a python construct representing the results. Although it functions as a list in different ways, it is actually what is called iterable. It just pokes fun at the behavior of the list so you can use things like for-loops on it.

Tuple : fixed list. This means that after installing it, it cannot be changed. In almost any other way, it behaves exactly like a list.

Dictionary . Also known as a hash in other languages. It can be considered a “key list”. A “list” in the strictest sense is a group of objects stored sequentially in memory. In the old days of programming, you would have to “pull out” items and “click” items on a list, and you could only get them in FIFO or in the first order. Dictionaries provide a way to search for items in a list. It consists of key-value pairs, so you can refer to the key and get the attached value.

Now in terms of Django templates:

QuerySets : you iterate over them using standard methods. Having received the result from MyModel.objects.all() , you can use the {% for value in queryset %} tag.

Tuples . As for iterations, they behave like standard lists. You can also just use the {% for value in tuple %} tag. The only potential hang is that sometimes you get tuples of tuples or a list of tuples. They look like multi-level lists. You have to iterate over the external list or tuple, and then iterate over the internal ones.

Dictionaries : they are probably the most complex, just because they need a method call to get an iterative 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 a simple iteration in a Django template:

 {% for value in mydict.itervalues %} 
+9
source

All Articles