Render nested queryset in a template ?

41 views
Skip to first unread message

kooliah

unread,
Jul 18, 2011, 3:29:17 PM7/18/11
to django...@googlegroups.com
I need to render a nested queryset in a template, a classic father/son
structure like
publisher>dvds>renters,

code
-------------------------------------------------------------------------------
class publisher(models.Model):
publisherid = models.IntegerField(primary_key=True)
publishername = models.CharField(max_length=135, blank=True)

class dvds(models.Model):
dvdsid = models.IntegerField(primary_key=True)
dvdsname = models.CharField(max_length=135,verbose_name='Corp.Name')
publisher_publisherid = models.ForeignKey(publisher,
db_column='publisher_publisherid')

class renters(models.Model):
rentersid = models.IntegerField(primary_key=True)
rentersname = models.CharField(max_length=135, blank=True)
dvds_dvdsid = models.ForeignKey(dvds, db_column='dvds_dvdsid')
-------------------------------------------------------------------------------

in python code i can use this code:

code
-------------------------------------------------------------------------------
pub= publisher.objects.get(publisherid=1)
for disk in pub.dvds_set.all():
print disk.dvdsname
for rent in disk.renters_set.all():
print rent.rentersname
-------------------------------------------------------------------------------

How can i do same things in a template ?
Can i call methods in a tag or i have to build a dictionary and pass it
to template?

Thanks to all

Daniel Roseman

unread,
Jul 18, 2011, 5:57:25 PM7/18/11
to django...@googlegroups.com
Op maandag 18 juli 2011 20:29:17 UTC+1 schreef kooliah het volgende:
I need to render a nested queryset in a template, a classic father/son
structure like
publisher>dvds>renters,

<snip> 

in python code i can use this code:

code
-------------------------------------------------------------------------------
pub= publisher.objects.get(publisherid=1)
for disk in pub.dvds_set.all():
     print disk.dvdsname
     for rent in disk.renters_set.all():
         print rent.rentersname
-------------------------------------------------------------------------------

How can i do same things in a template ?
Can i call methods in a tag or i have to build a dictionary and pass it
to template?

Thanks to all


You can do exactly that, except without the () brackets. You'll need to pass the `pub` object into the template context, but the for loops work fine within the template.
--
DR. 

kooliah

unread,
Jul 18, 2011, 7:53:20 PM7/18/11
to django...@googlegroups.com
Thank you
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Bm0QDiljOakJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages