Accessing related fields.

10 views
Skip to first unread message

Petey

unread,
Sep 11, 2011, 7:23:16 AM9/11/11
to django...@googlegroups.com
The more time I spend with django, the more problems pop up :)

I dont fully understand how to access related fields between models. (FK or ManyToMany)
Code:
http://pastebin.com/qbciYqYw

In code pasted below - {{p.souce.all}} returns source name but in that form:  [<Source: Google >], instead of "Google".
Also I dont know how to access source.url.
  1. {% for p in publisher.object_list %}
  2.         {{ p.date_added|date:"d-m-Y" }} r.
  3.         {{ p.source.all }}
  4. <br>
  5. {{ p.type|title }}  
  6. {% endfor %}

Could you explain me how does that work? I couldn't figure it out only by documentation, I need some examples :)

Daniel Roseman

unread,
Sep 11, 2011, 8:28:34 AM9/11/11
to django...@googlegroups.com
It should be obvious that `p.source.all` returns *all* the related `source` objects - so it returns a Queryset, which is iterable. If you want to access the first item in the list, then you can slice it, like any other list. Because of the special syntax restrictions in templates, you can do `p.source.all.0` (rather than `p.source.all()[0]` which is what you would do in Python code).
--
DR.

Petey

unread,
Sep 11, 2011, 8:36:25 AM9/11/11
to django...@googlegroups.com
That worked but how do I acces other field called "url" which is in Category model?

Daniel Roseman

unread,
Sep 11, 2011, 8:38:29 AM9/11/11
to django...@googlegroups.com
Why would it be any different? You have the same relationship between Publisher and Category as you do between Publisher and Source, so just do the same thing again.
--
DR.

Petey

unread,
Sep 11, 2011, 8:38:39 AM9/11/11
to django...@googlegroups.com
And also how do I display more objets from that list? ;) Sorry for double post

Martin Tiršel

unread,
Sep 11, 2011, 8:44:38 AM9/11/11
to django...@googlegroups.com
Hi,

as Daniel wrote, the {{ p.source.all }} is a queryset you can access the
same way as in {% for p in publisher.object_list %}. So you create a
nested for loop.

Martin

Petey

unread,
Sep 11, 2011, 8:45:53 AM9/11/11
to django...@googlegroups.com
Ooops I meant source ;)

Firstly:
{{ p.source.all.0 }}  - < gets source name, only first element (as proper slice should work) but I still dont understand how to get all avaliable elements for each source in publisher

Secondly:
{{ p.source.url.all.0 }} < does not get URL from Source.url, I really need an example on accessing other fields

Martin Tiršel

unread,
Sep 11, 2011, 8:50:19 AM9/11/11
to django...@googlegroups.com

{{ p.source.all }} - is a list of source objects

{{ p.source.all.0 }} - is the first item in the list of source objects
(the same as p.source.all[0] in python)

{{ p.source.all.0.some_attribute }} - you access some_attribute of the
object source which is the first item in the list od source objects

Martin

Petey

unread,
Sep 11, 2011, 8:57:00 AM9/11/11
to django...@googlegroups.com
Thank you Bruce and Daniel both anwsers really helped me.
I really appreciate it :).
Reply all
Reply to author
Forward
0 new messages