Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27511 closed Bug (fixed)

Docs incorrect about related_model for GenericRelation being None

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

Description

The documentation says for field.related_model:
If a field has a generic relation (such as a GenericForeignKey or a GenericRelation) then related_model will be None.

However this is not true for GenericRelations. For these fields the related_model points to the model the field relates to just like the reverse of a ForeignKey. The documentation should be fixed, however this means that to the best of my knowledge, there is no way to distinguish between GenericRelation and reverse of a ForeignKey(short of checking if the field is an instance of GenericRelation).

Attachments (1)

27511.diff (721 bytes ) - added by Tim Graham 8 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 by Tim Graham, 8 years ago

Component: UncategorizedDocumentation
Has patch: set
Summary: related_model for GenericRelation is not NoneDocs incorrect about related_model for GenericRelation being None
Triage Stage: UnreviewedAccepted

Is the attached patch fine?

by Tim Graham, 8 years ago

Attachment: 27511.diff added

comment:2 by Amir Keivan Mohtashami, 8 years ago

Since GenericRelation is actually a generic field, isn't it better to explicitly exclude it from the rule?

comment:3 by Tim Graham, 8 years ago

In the patch, I meant to write, "If a field is a GenericForeignKey". (chop extra "generic")

I'm not sure if your comment is suggesting a documentation change or a code change?

comment:4 by Amir Keivan Mohtashami, 8 years ago

The change you propose will correctly describe the current behavior of Django.

However I don't know whether it is the right approach to change the documentation or would it be better to update the code. Since related_model is set to None for GenericForeignKey it might be reasonable to set it to None for GenericRelation as well.

comment:5 by Simon Charette, 8 years ago

Since related_model is set to None for GenericForeignKey it might be reasonable to set it to None for GenericRelation as well.

GenericForeignKey.related_model is set to None as it can point to many models by the generic nature of the foreign key. GenericRelation always point to a single reverse model so I believe things should be kept this way.

comment:6 by Amir Keivan Mohtashami, 8 years ago

I see. Then I guess after removing the additional "generic" the proposed patch should be enough.

comment:7 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In 55adfc0:

Fixed #27511 -- Corrected Field.related_model docs about GenericRelation.

comment:8 by Tim Graham <timograham@…>, 8 years ago

In 59ea6fd:

[1.10.x] Fixed #27511 -- Corrected Field.related_model docs about GenericRelation.

Backport of 55adfc076030fc6be2c8d459c4c0a5c91cd4c94c from master

Note: See TracTickets for help on using tickets.
Back to Top