.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 doesn't know which one to give you).  In this case you may
actually want get, since I presume that there should only be one Customer
per User.

Just for completeness, context[0] is similar to context.get().  It will
still raise an error if there are no such Customer objects, but it won't
complain if there are more than 1.

And, yes, the decision of which field is shown in what order should
probably be in the template, since it is very much presentation, rather
than logic, so passing in just the one instance and using things like {{
data.field_name }} is likely the way to go.


On Wed, Sep 18, 2013 at 3:58 PM, Johan Bergkvist <jbergkvi...@gmail.com>wrote:

> 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 forwards and backwards
> relations on instances, right? Which, if I understand you correctly I only
> get when omitting the .get() method.
> I probably should have started out with a Python tutorial before jumping
> in to django :) Well, no pain no gain I guess.
> thanks by the way.
>
> Den onsdag den 18. september 2013 20.14.47 UTC+2 skrev 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() method. There's not really a good way of iterating over all the
>> fields in an instance with their names (you can muck about with the stuff
>> in _meta, but I wouldn't recommend it).
>>
>> As I say though, in practice that's rarely a problem as you usually want
>> to reference individual fields by name, rather than iterating over them.
>> --
>> DR.
>>
>>
>> On Wednesday, 18 September 2013 16:39:34 UTC+1, Johan Bergkvist wrote:
>>>
>>> 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 %}
>>>                    no data
>>>           {% endfor %}
>>> {%endfor%}
>>> ?
>>>
>>>
>>>
>>>
>>> Den onsdag den 18. september 2013 00.02.21 UTC+2 skrev 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
>>>> iterating through each item in the queryset, while the inner loop iterates
>>>> over the elements in each dict.
>>>>
>>>> Reverse relations work with a model instance. But you don't have one of
>>>> those - you've got a dict, because you used .values(). If you dropped that
>>>> call, you'd have an instance which you could use the backwards relation on,
>>>> but you wouldn't be able to iterate over the fields with .items(). Note
>>>> that most of the time you don't need to do that, so mostly you would use
>>>> the instance rather than calling .values().
>>>>
>>>> --
>>>> DR.
>>>>
>>>>  --
> 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 to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
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 to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to