Many to many in template question

251 views
Skip to first unread message

mail....@gmail.com

unread,
Jun 6, 2007, 4:42:29 PM6/6/07
to Django users
I know I'm making this harder than it has to be, but the logic is just
escaping me.

I've got two models, say "Foo" and "Bar"

"Bar" establishes a manyToManyField relationship with "Foo":
foos = models.ManyToManyField(Foo,
filter_interface=models.HORIZONTAL, null=True, blank=True)

I'm using a generic detail template for Foo, and want to include the
matching Bars.
Since Bar has the mtm and Foo doesn't, I figured Bar was aware of Foo,
but not the other way around, and therefore I would need a template
tag.

So assuming my logic so far is OK, how should I get the correct Bar
objects (the ones that have a matching Foo in foos)?

chin...@gmail.com

unread,
Jun 6, 2007, 5:05:28 PM6/6/07
to Django users
This doesn't exactly answer your question (because I'm a huge noob
still), but M2M relationships work in both directions:
http://www.djangoproject.com/documentation/db-api/#many-to-many-relationships

I think to access it in the template, you'd just need to pass the
information from your view?


On Jun 6, 1:42 pm, "bax...@gretschpages.com" <mail.bax...@gmail.com>
wrote:

mail....@gmail.com

unread,
Jun 7, 2007, 9:37:32 AM6/7/07
to Django users
That's what I thought, too, but I can't seem to access it. Maybe I
need to just write a custom view for this page.

On Jun 6, 4:05 pm, "chinka...@gmail.com" <chinka...@gmail.com> wrote:
> This doesn't exactly answer your question (because I'm a huge noob

> still), but M2M relationships work in both directions:http://www.djangoproject.com/documentation/db-api/#many-to-many-relat...

Russell Keith-Magee

unread,
Jun 7, 2007, 9:58:17 AM6/7/07
to django...@googlegroups.com
On 6/7/07, bax...@gretschpages.com <mail....@gmail.com> wrote:
>
> That's what I thought, too, but I can't seem to access it. Maybe I
> need to just write a custom view for this page.

Based on what you've said so far, you shouldn't need to write a new
view. You just need to reference the m2m data correctly. Assuming you
have the models:

class Foo(models.Model):
name = models.CharField()

class Bar(models.Model):
name = models.CharField()
foos = models.ManyToManyField(Foo)

Then you can, in view code, ask for:
>>> Foo.objects.all()
>>> Bar.objects.all()
>>> mybar.foos.all()
>>> myfoo.bar_set.all()

And in template, if you have mybar and myfoo into the context, you can write:
{{ mybar }}
{{ myfoo }}
{% for foo in mybar.foos.all %}
{{ foo.name }}
{% endfor %}
{% for bar in myfoo.bar_set.all }
{{ bar.name }}
{% endfor %}

Yours,
Russ Magee %-)

mail....@gmail.com

unread,
Jun 7, 2007, 1:09:40 PM6/7/07
to Django users
I'm just getting more and more stumped.

I went ahead and created a custom view for Foo, so that's taken care
of, but Bar (which should be easy) isn't working either.

I have:
class Bar(models.Model):
name= models.CharField(maxlength=100)
foos = models.ManyToManyField(Foo,
filter_interface=models.HORIZONTAL, related_name="foos", null=True,
blank=True)
slug = models.SlugField(prepopulate_from=('name',))

In passing it in a generic view, in the template, shouldn't I just be
able to do this?

{% for foo in bar.foos_set.all %}
test
{% endfor %}

I get nothing. If I put in {{ bar.foos_set.all }} I get an object
pointer, not a list.
I haven't thrown any errors when I ran syncdb or validate.

On Jun 7, 8:58 am, "Russell Keith-Magee" <freakboy3...@gmail.com>
wrote:

mail....@gmail.com

unread,
Jun 7, 2007, 1:27:52 PM6/7/07
to Django users
Figured it out. Template wanted (for whatever reason)
{% for foo in bar.foos.all %} instead of {% for foo in
bar.foos_set.all %}

On Jun 7, 12:09 pm, "bax...@gretschpages.com" <mail.bax...@gmail.com>
wrote:

Russell Keith-Magee

unread,
Jun 7, 2007, 8:09:25 PM6/7/07
to django...@googlegroups.com
On 6/8/07, bax...@gretschpages.com <mail....@gmail.com> wrote:
>
> Figured it out. Template wanted (for whatever reason)
> {% for foo in bar.foos.all %} instead of {% for foo in
> bar.foos_set.all %}

This would all be a lot clearer if you would read the DB-api docs. The
related-objects section explains this aspect of Django in great
detail.

http://www.djangoproject.com/documentation/db-api/#related-objects

In short:
Your Bar model has a foos attribute. Therefore, bar.foos.all().
Your Foo model has an implied attribute, because it is on the
receiving end of a m2m relation with Bar. Therefore,
foo.bar_set.all().

Yours,
Russ Magee %-)

mail....@gmail.com

unread,
Jun 8, 2007, 3:43:53 PM6/8/07
to Django users
Thanks Russell. I'll go re-read them (again). One of these days
they'll actually sink into my thick skull.

On Jun 7, 7:09 pm, "Russell Keith-Magee" <freakboy3...@gmail.com>
wrote:

Reply all
Reply to author
Forward
0 new messages