M2M Magic Accessors without explicit ManyToMany Field

19 views
Skip to first unread message

Clayton D

unread,
May 8, 2018, 11:18:57 AM5/8/18
to Django users
I posted a question on SO (https://stackoverflow.com/q/50222402/1978687), but it's been viewed very few times so I'm hoping this is the right place to get attention from those "in the know".  The complete question (and progress so far) is documented on SO so that's probably the best place to answer.

The essentials of the question are:
  • I need to create an M2M relationship between two models, but...
  • The models are from external libraries (trivially User and Car)
  • The models are non-abstract and multi-table inheritance would introduce a ton of unnecessary overhead
  • Others will use my code like (if not literally as) a 3rd party library so I want to provide all of the usual magic methods for usability -- e.g. Cars.objects.filter(users=user) -- which doesn't happen if I create a manual M2M (i.e. "through") table.
A commenter suggested using `add_to_class` on one of the 3rd party models.  It passes trivial test cases, but has hair and some of the magic breaks because it's treated as a `through=` relationship even though it's a trivial M2M table.

I'm hoping someone who understands the ManyToMany implementation can tell me if I can hijack the `contribute...` methods (or something similar) to get this job done.

Thanks!

Vijay Khemlani

unread,
May 8, 2018, 1:17:16 PM5/8/18
to django...@googlegroups.com
Why would it be a muti table inheritance? 

I would just inherit Car (class MyCar) and add the m2m field there, seems way more explicit and easy to understand too.

--
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.
To post to this group, send email to 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/cc6f6c74-e5df-4fcf-b473-42f894fd3abb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages