M2M Question

26 views
Skip to first unread message

Matthew Pava

unread,
Dec 15, 2016, 4:53:59 PM12/15/16
to django...@googlegroups.com

I have a Document model that has an M2M-symmetrical field on itself.  The idea is that we can “link” documents to other documents.  It’s very simple to find the linked documents of a particular document, but is there an easy way to get all the linked documents of the linked documents of the document in question?  I will probably have to write my own QuerySet or ModelManager function, but I was just curious if Django had something built in.

 

For example:

A is linked to J, C, K.

è J is linked to A

è C is linked to A

è K is linked to A

B is linked to J, M, P.

è J is linked to B

è M is linked to B

è P is linked to B

C is linked to O.

è O is linked to C

D is linked to O, P, R.

è O is linked to D

è P is linked to D

è R is linked to D

 

So when I get the entire “chain” of documents linked to A, I would get something like this:

J, C, K, B, O, D, P, R, M

 

Since J is linked to A and B, K is linked to A, and C is linked to A and O and O is linked to D, and D is linked to O and P, R is linked to D and P is linked to B which is linked to M (and J).

Hmmm…my contrived example exploded on me more than I intended…

Mike Dewhirst

unread,
Dec 15, 2016, 5:34:07 PM12/15/16
to django...@googlegroups.com
On 16/12/2016 8:52 AM, Matthew Pava wrote:
>
> I have a Document model that has an M2M-symmetrical field on itself.
> The idea is that we can “link” documents to other documents. It’s
> very simple to find the linked documents of a particular document, but
> is there an easy way to get all the linked documents of the linked
> documents of the document in question? I will probably have to write
> my own QuerySet or ModelManager function, but I was just curious if
> Django had something built in.
>
> For example:
>
> A is linked to J, C, K.
>
> èJ is linked to A
>
> èC is linked to A
>
> èK is linked to A
>
> B is linked to J, M, P.
>
> èJ is linked to B
>
> èM is linked to B
>
> èP is linked to B
>
> C is linked to O.
>
> èO is linked to C
>
> D is linked to O, P, R.
>
> èO is linked to D
>
> èP is linked to D
>
> èR is linked to D
>
> So when I get the entire “chain” of documents linked to A, I would get
> something like this:
>
> J, C, K, B, O, D, P, R, M
>

I would put a method on the document model to return a queryset with
respect to itself. To do this I would nominate the "through" table and
have a model class which maps to it. That model will have two PKs to the
document model. The document model method(s) which return querysets
query the "through" table.

Also have a look at select_related

https://docs.djangoproject.com/en/1.10/ref/models/querysets/#select-related

> Since J is linked to A and B, K is linked to A, and C is linked to A
> and O and O is linked to D, and D is linked to O and P, R is linked to
> D and P is linked to B which is linked to M (and J).
>
> Hmmm…my contrived example exploded on me more than I intended…
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/7a3247baf2234f8ab3a7a4db40bcf8c5%40ISS1.ISS.LOCAL
> <https://groups.google.com/d/msgid/django-users/7a3247baf2234f8ab3a7a4db40bcf8c5%40ISS1.ISS.LOCAL?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages