Thank you, Daniel. That makes sense now. If the full objects are already there, then this should work great! L J
On Apr 17, 4:07 am, Daniel Roseman <dan...@roseman.org.uk> wrote: > On Tuesday, 17 April 2012 07:10:35 UTC+1, LJ wrote: > > > I am having trouble figuring out how to query the database and return > > the results in a format that my template can render appropriately. > > I have a model that has a ManyToMany field: > > > class Student() > > ... > > parents = models.ManyToManyField('parents.Parent', blank=True, > > null=True) > > ... > > The Parent model looks like: > > > class Parent() > > ... > > first_name models.CharField(max_length=30) > > last_name models.CharField(max_length=30) > > gender models.CharField(max_length=1) > > ... > > def __unicode__(self): > > return u'Parent : %s %s' % (self.first_name, self.last_name) > > > The method in my view currently looks something like this: > > > def get_parents( request, template ) > > id=request.GET['id'] > > template_data["parents"] = Parent.objects.filter(student=id) > > return render_to_response( template, template_data, > > context_instance=RequestContext(request)) > > > The template data is returning the data in the format: > > [ <Parent: Parent: Bob Thomas>, <Parent: Parent: Mary Thomas>] > > > Instead, I need the template data formatted with the other fields in > > my Parent model, like: > > [ <Parent: { id: 3, first_name: Bob, last_name: Thomas}>, > > <Parent: { id: 4, first_name: Mary, last_name: Thomas}> ] > > > The format doesn't have to be exactly like the above, but I need to > > include the index, and to return some of the other fields defined in > > my Parent model. > > My template will look something like: > > {% for parent in parents.object_list %} > > <tr> > > <td>{{parent.id}}</td> > > <td>{{parent.first_name}}</td> > > <td>{{parent.last_name}}</td> > > {% endfor %} > > > Can someone give me some ideas about how I can change my view to > > return my template data in a more useable format? > > This has nothing to do with your view, which is fine. The query isn't > returning the data like that - you're just showing a string representation > of the queryset, created by calling `repr()` (which calls `unicode()`) on > each parent. The objects are there in full. > > Your proposed template code is correct, except that you should iterate > through just `parents`, not "parents.object_list". > -- > DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.