Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#29458 closed Bug (fixed)

Documentation incorrectly says Model._meta.get_field() uses related_name instead of related_query_name

Reported by: Tomasz Knapik Owned by: Jeff
Component: Documentation Version: 2.1
Severity: Normal Keywords:
Cc: Aram Dulyan Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tomasz Knapik)

The documentation seems to mention that get_field should use related_name, but when related_query_name is defined, it uses that instead.

Let's say in situation like this...

from django.db import models


class ModelA(models.Model):
    test_model = models.ForeignKey('ModelB', models.CASCADE,
                                   related_name='model_a_related_name',
                                   related_query_name='model_a_related_q_name')


class ModelB(models.Model):
    some_field = models.CharField(max_length=255)

Using get_field with value from related_name.

>>> from testapp.models import ModelB
>>> ModelB._meta.get_field('model_a_related_name')
Traceback (most recent call last):
  File "/home/tomaszk/Projects/testsite/venv/lib64/python3.6/site-packages/django/db/models/options.py", line 566, in get_field
    return self.fields_map[field_name]
KeyError: 'model_a_related_name'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/tomaszk/Projects/testsite/venv/lib64/python3.6/site-packages/django/db/models/options.py", line 568, in get_field
    raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: ModelB has no field named 'model_a_related_name'
>>> 

But this will work.

>>> ModelB._meta.get_field('model_a_related_q_name')
<ManyToOneRel: testapp.modela>

Change History (7)

comment:1 by Tomasz Knapik, 6 years ago

Description: modified (diff)

comment:2 by Aram Dulyan, 6 years ago

Cc: Aram Dulyan added

comment:3 by Tim Graham, 6 years ago

Component: UncategorizedDocumentation
Summary: get_field uses related_query_name instead of related_nameDocumentation incorrectly says Model._meta.get_field() uses related_name instead of related_query_name
Triage Stage: UnreviewedAccepted

I haven't investigated in detail but I imagine the documentation should be updated.

comment:4 by Jeff, 6 years ago

Owner: changed from nobody to Jeff
Status: newassigned

I've confirmed the reported behavior and agree the docs seem misleading. I'll make a patch.

comment:5 by Jeff, 6 years ago

Has patch: set

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

Resolution: fixed
Status: assignedclosed

In 2d6776f:

Fixed #29458 -- Doc'd how related_query_name affects Model._meta.get_field().

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

In ddd9272c:

[2.1.x] Fixed #29458 -- Doc'd how related_query_name affects Model._meta.get_field().

Backport of 2d6776ffe0b58f729f4372635b49a59da99ca206 from master

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