Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango regroup many to many field
    primarykey
    data
    text
    <p>I'm having some trouble making this work on the template, wonder if I could get some help:</p> <p>I have a table with plants in it, and the plant table has a many-to-many relationship with the category table. Such that a plant can be in one or more categories. I'm using Django 1.5, here are the models:</p> <pre><code>class Plant(models.Model): scientific_name = models.CharField(max_length=128, unique=True) category = models.ManyToManyField(Category) ... class Category(models.Model): category = models.CharField(max_length=128) ... </code></pre> <p>And the view:</p> <pre><code>class PlantListView(ListView): context_object_name='plant_list' template_name='plants/index.html' model = Plant def get_queryset(self): return self.model.objects.all().order_by('category') </code></pre> <p>I've tried this:</p> <pre><code>{% regroup plant_list by category as category_list %} &lt;ul&gt; {% for category in category_list %} &lt;li&gt;{{ category.grouper }} &lt;ul&gt; {% for plant in plant_list.list %} &lt;li&gt;{{ plant.scientific_name }}&lt;/li&gt; {% endfor %} &lt;/ul&gt; &lt;/li&gt; {% endfor %} &lt;/ul&gt; </code></pre> <p>But all I get is:</p> <pre><code>&lt;django.db.models.fields.related.ManyRelatedManager object at 0x1053bfb90&gt; &lt;django.db.models.fields.related.ManyRelatedManager object at 0x1053bfbd0&gt; &lt;django.db.models.fields.related.ManyRelatedManager object at 0x1053bfd50&gt; </code></pre> <p>Of course, what I really want is:</p> <ul> <li>Annuals <ul> <li>Ageratum houstonianum </li> <li>Abutilon hybridum </li> <li>Acalypha hispida</li> </ul></li> <li>Bulbs Perennial <ul> <li>Ageratum houstonianum</li> <li>Allium giganteum </li> <li>Allium karataviense</li> </ul></li> </ul> <p>... etc ...</p> <p>Note that the same plant can exist in multiple categories, and should be listed in each.</p> <p>What am I doing wrong?</p> <p>Thanks in advance!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload