On Mar 30, 6:08 am, Daniel <unagimiy...@gmail.com> wrote: > Hi there, > > I'm in a template that is trying to simply show everything in my > database. But I can't seem to do it even though this should be dead > simple :<. > > Here is my view, called browse_all: > > def browse_all(request): > resultSet = Sample.objects.all(); > print resultSet > return render_to_response('browse_all.html',{'resultSet' : > resultSet}) > > Here is my template called "browse_all.html" > > {% extends "base.html" %} > {% block title %} View All Samples {%endblock%} > {% block page_title %} View All Samples {%endblock%} > > {% block content %} > <table> > {% for result in resultSet %} > <tr> > <td>sample #: {{ result.number}}</td> > <td> facet:{{result.facet.name}}</td> > <td> value: {{result.facet.value.name}}</td> > <td> description: {{result.description }}</td> > </tr> > {% endfor %} > </table> > {% endblock %} > > Here are my models: > > from django.db import models > > # Create your models here. > #defining these classes in the right order matters > > class Value(models.Model): > name = models.CharField(max_length=60) > > def __unicode__(self): > return self.name > > class Facet(models.Model): > name = models.CharField(max_length=60) > value = models.ForeignKey(Value) > > def __unicode__(self): > return self.name > > class Sample(models.Model): > number = models.CharField(max_length=30) > facet = models.ManyToManyField(Facet) > description = models.TextField() > > def __unicode__(self): > return u'%s' %(self.number,) > > class Meta: > ordering = ['number'] > > Basically, in my template it chokes trying to display the facet > property of the Sample class. I'm pretty sure it's b/c it's a many to > many field, but how can I get the facet displayed? Thanks!
Obviously you can't do 'sample.facet' because each sample has *multiple* facets - because as you say it's a manytomany relationship. So you'll need to iterate through the facets for each sample: {% for facet in result.facets.all %} -- 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-us...@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.