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+unsubscr...@googlegroups.com
<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to django-users@googlegroups.com
<mailto:django-users@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.
--
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/42fa7086-0cc7-db66-afc2-01ce8089f3a1%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.