62

What is the difference between Django's models.ManyToManyField and models.ManyToManyRel? I'm confused about this stuff.

Opal
  • 81,889
  • 28
  • 189
  • 210
sultan
  • 5,978
  • 14
  • 59
  • 103

2 Answers2

78

ManyToManyRel is used by the ManyToManyField to implement the relationship object for the Field base class which it extends. If you were to create a new field class that extended the Field class and contained a many-to-many relationship you might find this class convenient but it should not be used in your models (which is where you will see the pop-up suggestion if your editor lists available calls).

See class Field @: https://github.com/django/django/blob/master/django/db/models/fields/__init__.py class ManyToManyRel & class ManyToManyField @: https://github.com/django/django/blob/master/django/db/models/fields/related.py

I am glad that the vast majority of the questions here are questions that can be answered by looking at reference material and documentation. Researching and sharing ideas and digging into code that is "not for external use" is the fun. I know how to start answering this question, if i didn't i would not have written anything. Good question dude!

Damon
  • 936
  • 7
  • 3
-19

If you discovered ManyToManyRel by digging into the source code, you can read the docstrings for the class. It's not documented anywhere - on purpose, because it's not for external use. It is certainly not meant for defining actual field relationships between models.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 11
    I've looking over the sources and figured out that `ManyToManyRel` is used for relation lookup by `ManyToManyField`. Also I follow the actual documentation as accurate as possible). – sultan Mar 08 '11 at 06:32
  • 106
    Just a heads up, the asker could have seen it pop up in an ide's auto complete box. – George Griffin Oct 13 '11 at 02:50
  • 16
    If we always stuck to just the docs, most of us would be really lost. – explodes Apr 19 '12 at 18:02
  • 12
    I had the same question using Eclipse + PyDev and finding the undocumented ManyToManyRel suggestion. It's not a trivial question at all. – chirale Oct 17 '12 at 12:16
  • 3
    Wow... because of this answer I just realized there are no private methods (or anything else) in Python. Crazy, I tell you, crazy! – Eduard Luca Jun 16 '13 at 21:58
  • 1
    @George Griffin, it did happen to me too. A popup which suddenly confuses as to what the correct choice is. – unlockme Aug 22 '16 at 19:04