Thank you, Phang. I was not familiar with the locals() method, so I may look into this. L J
On Apr 17, 2:22 am, Phang Mulianto <braveh...@gmail.com> wrote: > On Tue, Apr 17, 2012 at 2:10 PM, LJ <ljayad...@gmail.com> 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>] > > > I think this because you return your result in template_data["parents"] , > > so in your tempalte it will be parent:parent:bob thomas. > > usualy i use locals() to pass all the var to template. > > return render_to_repsonse(template, > locals(),context_instance=RequestContext(request)) > > don't know if this is best practice in django, but make me easier to work > with variables need to pass to template without have to include it in an > array. > > with the locals() you will have the data in your template like you mention > below. > > > > > > > > > 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? > > > -- > > 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. -- 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.