Access to Many-To-Many intermediary table from django

2,180 views
Skip to first unread message

nucles

unread,
Dec 10, 2008, 9:22:16 AM12/10/08
to Django users
Hello,

If we create 2 models with many-to-many relationship django creates
the intermediary table automatically for us.
How can i use this intermediate table to relate other entities? Can i
access to the primary field of the intermediary table?
Should i create two One-to-Many relations manually and then use the
intermediary table?

Thank you for any suggest

Regardes,
Dennis

Malcolm Tredinnick

unread,
Dec 10, 2008, 9:04:20 PM12/10/08
to django...@googlegroups.com

On Wed, 2008-12-10 at 06:22 -0800, nucles wrote:
> Hello,
>
> If we create 2 models with many-to-many relationship django creates
> the intermediary table automatically for us.
> How can i use this intermediate table to relate other entities?

What do you mean? Normally you don't need to worry about the
intermediate table at all; it's an implementation detail at the database
level.

So what is the problem you're trying to solve here?

Regards,
Malcolm


Elvis Stansvik

unread,
Dec 11, 2008, 3:40:17 AM12/11/08
to django...@googlegroups.com
2008/12/10 nucles <denys.b...@krankikom.de>:
>
> Hello,
>
> If we create 2 models with many-to-many relationship django creates
> the intermediary table automatically for us.
> How can i use this intermediate table to relate other entities? Can i
> access to the primary field of the intermediary table?
> Should i create two One-to-Many relations manually and then use the
> intermediary table?

No, you should still use a ManyToManyField, but use the 'through'
argument to specify the intermediate model, on which you should have
two ForeignKey fields referencing each of your two models. This way
you will have full control over the intermediate table. Read more
about this at:

http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships

I'll have to quietly ask the same question as Malcolm though; is this
really what you want?

Regards,
Elvis

Elvis Stansvik

unread,
Dec 11, 2008, 3:43:40 AM12/11/08
to django...@googlegroups.com
2008/12/11 Malcolm Tredinnick <mal...@pointy-stick.com>:
I agree this question should be asked, but there are valid use cases.
I recently had a Book model and Contributor model which I joined with
a Contribution table, which in turn had a ForeignKey into a
ContributitionType model. This lets me add more types of contributions
to a book in the future, such as author, editor, translator,
illustrator et.c.

Regards,
Elvis

>
> Regards,
> Malcolm
>
>
>
> >
>

Malcolm Tredinnick

unread,
Dec 11, 2008, 7:44:40 PM12/11/08
to django...@googlegroups.com

Yes, I know about that kind of situation. If the original poster meant
"adding extra attributes to the intermediate table to link to other
tables" when he said "relate other entities" then using "through" on
ManyToManyField or doing it manually is a good answer. No problems
there. But it depends on whether that's the intended interpretation. I
was asking for a rephrasing of the question, in more concrete terms --
hence a description of the actual problem instead of something abstract
-- to work out what the question really was.

If he's doing what you're guessing, then I'm behind you 100%. Your
answer is correct. If not, well... :-)

Regards,
Malcolm


Reply all
Reply to author
Forward
0 new messages