Code

Opened 4 years ago

Closed 3 weeks ago

Last modified 3 weeks ago

#14481 closed Cleanup/optimization (fixed)

better documentation of the default m2m "through" class

Reported by: jonathanmorgan Owned by: nobody
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It would be polite to document somewhere the exact characteristics of the default m2m "through" class. I looked today and could not find that anywhere in the documentation.

Specifically, I think you want something like this in the Model page where it talks about ManyToManyFields:

When you create a ManyToManyField relation without a "through" argument, there is still a "through" class and table created to hold the association.

The class is:

<model_that_declares_field>.<ManyToManyField_name>.through

This is a model instance for a table with three fields:

  • id
  • <containing_model>_id
  • <other_model>_id

This class can be used to query associated records for a given model instance, and can also be used in static methods (like m2m_changed handlers) to query the association table to pull in instances that are associated either with Model A or Model B so that you can process them. The through class for a given association is the class that raises the m2m_changed signal, and so it is the class you need to associate an m2m_changed signal handler with, not the Model that contains the association.

It is also dynamically generated when the Model class that declares the ManyToManyField relation is imported, so you can only access "through" classes associated with a given pair of models after importing the model that declares the ManyToManyField (and you won't be able to access it if you only import the model that is the "other" side of the relation).

Attachments (0)

Change History (8)

comment:1 Changed 4 years ago by gabrielhurley

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Related to but separate from #13586. Let's make sure these two don't end up duplicating each other in content.

comment:2 Changed 3 years ago by julien

  • Severity set to Normal
  • Type set to Cleanup/optimization

comment:3 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:4 Changed 2 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:5 Changed 3 weeks ago by jorgecarleitao

  • Has patch set

comment:6 Changed 3 weeks ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In de9012907063e210e97c0d65cde312ff6174d420:

Fixed #14481 -- Documented implicit "through" class created by ManyToManyField.

Thanks to jonathanmorgan for the report and initial patch.

comment:7 Changed 3 weeks ago by Tim Graham <timograham@…>

In 807aadaaad317b67965052048b3a2fb92a1aaa79:

[1.7.x] Fixed #14481 -- Documented implicit "through" class created by ManyToManyField.

Thanks to jonathanmorgan for the report and initial patch.

Backport of de90129070 from master

comment:8 Changed 3 weeks ago by Tim Graham <timograham@…>

In 6b52e1793c682d4f9c8f030dcbe6908ddddf875e:

[1.6.x] Fixed #14481 -- Documented implicit "through" class created by ManyToManyField.

Thanks to jonathanmorgan for the report and initial patch.

Backport of de90129070 from master

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.