ManyToManyField.through for what?

62 views
Skip to first unread message

Erik Bernoth

unread,
Jan 18, 2009, 10:20:04 AM1/18/09
to Django users
Hi guys,
I'm trying to create a db model in Django. And until now I created n:m-
Relations with own tables, because in many cases I need extra
information like the creation date of that connection-entity. And now
I found in the docs that it is possible to connect these individual
n:m tables of myself with the foreign-key tables. But all together it
does not look like I can do more with that .through attribute as I
could without.

I want to explain my question a little more in detail.For reference
look here: http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships

In this example a "Membership" Table is created like I created my own
n:m-Tables. But than the following line is added to Group:
members = models.ManyToManyField(Person, through='Membership')

It looks like a violation to the DRY-Principle, to put this line
there.
What happens if I don't put this line in my code?

I hope this is not an all day question... I couldn't find an answer in
the docs and the mail group.

kind regards
Erik Bernoth

Ramiro Morales

unread,
Jan 18, 2009, 11:27:06 AM1/18/09
to django...@googlegroups.com
On Sun, Jan 18, 2009 at 1:20 PM, Erik Bernoth <sho...@gmx.net> wrote:
>
> Hi guys,
> I'm trying to create a db model in Django. And until now I created n:m-
> Relations with own tables, because in many cases I need extra
> information like the creation date of that connection-entity. And now
> I found in the docs that it is possible to connect these individual
> n:m tables of myself with the foreign-key tables. But all together it
> does not look like I can do more with that .through attribute as I
> could without.
>

Take a look at ticket #6095 to know the motivation and rationale behind
the addition of the through option and some of the advantages when
compared with using you own model with FKs to the two related models.

Also and the documentation you pointed to modeltests/m2m_through tests
show the usage of the new API that allows you to do the same things you
were able to do but in a clearer way.

Regards,

--
Ramiro Morales

Erik Bernoth

unread,
Jan 18, 2009, 11:46:49 AM1/18/09
to Django users
On 18 Jan., 17:27, Ramiro Morales <cra...@gmail.com> wrote:
Thanks very much. I see I have still other options where I can look
first: trac and unit tests. Will read all of that. :)

Ramiro Morales

unread,
Jan 18, 2009, 11:58:58 AM1/18/09
to django...@googlegroups.com
On Sun, Jan 18, 2009 at 2:46 PM, Erik Bernoth <sho...@gmx.net> wrote:
>
> Thanks very much. I see I have still other options where I can look
> first: trac and unit tests. Will read all of that. :)

Ideas and patches to enhance the documentation making this
more straight forward to understand for the next person stumbling
the same doubts are welcome 8)

--
Ramiro Morales

Reply all
Reply to author
Forward
0 new messages