[Django] #29458: get_field uses related_query_name instead of related_name

16 views
Skip to first unread message

Django

unread,
May 30, 2018, 12:58:24 PM5/30/18
to django-...@googlegroups.com
#29458: get_field uses related_query_name instead of related_name
-----------------------------------------+------------------------
Reporter: Tomasz Knapik | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
The
[https://docs.djangoproject.com/en/2.1/ref/models/meta/#django.db.models.options.Options.get_field
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...

{{{#!python
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)
pass
}}}
Using `get_field` with value from `related_name`.
{{{#!python
>>> 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.

{{{#!python
>>> ModelB._meta.get_field('model_a_related_q_name')
<ManyToOneRel: testapp.modela>
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29458>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 30, 2018, 1:05:21 PM5/30/18
to django-...@googlegroups.com
#29458: get_field uses related_query_name instead of related_name
-------------------------------+--------------------------------------

Reporter: Tomasz Knapik | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Tomasz Knapik:

Old description:

New description:

The
[https://docs.djangoproject.com/en/2.1/ref/models/meta/#django.db.models.options.Options.get_field
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...

{{{#!python
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`.


{{{#!python
>>> 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.

{{{#!python
>>> ModelB._meta.get_field('model_a_related_q_name')
<ManyToOneRel: testapp.modela>
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/29458#comment:1>

Django

unread,
Jun 4, 2018, 3:22:42 AM6/4/18
to django-...@googlegroups.com
#29458: get_field uses related_query_name instead of related_name
-------------------------------+--------------------------------------

Reporter: Tomasz Knapik | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Aram Dulyan):

* cc: Aram Dulyan (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/29458#comment:2>

Django

unread,
Jun 8, 2018, 7:55:36 PM6/8/18
to django-...@googlegroups.com
#29458: Documentation incorrectly says Model._meta.get_field() uses related_name
instead of related_query_name
-------------------------------+------------------------------------

Reporter: Tomasz Knapik | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 2.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Tim Graham):

* component: Uncategorized => Documentation
* stage: Unreviewed => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/29458#comment:3>

Django

unread,
Jun 28, 2018, 2:25:37 PM6/28/18
to django-...@googlegroups.com
#29458: Documentation incorrectly says Model._meta.get_field() uses related_name
instead of related_query_name
-------------------------------+------------------------------------
Reporter: Tomasz Knapik | Owner: Jeff
Type: Bug | Status: assigned

Component: Documentation | Version: 2.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Jeff):

* owner: nobody => Jeff
* status: new => assigned


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/29458#comment:4>

Django

unread,
Jun 28, 2018, 3:24:07 PM6/28/18
to django-...@googlegroups.com
#29458: Documentation incorrectly says Model._meta.get_field() uses related_name
instead of related_query_name
-------------------------------+------------------------------------
Reporter: Tomasz Knapik | Owner: Jeff
Type: Bug | Status: assigned
Component: Documentation | Version: 2.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Jeff):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/10105 #29458]

--
Ticket URL: <https://code.djangoproject.com/ticket/29458#comment:5>

Django

unread,
Jun 28, 2018, 8:02:35 PM6/28/18
to django-...@googlegroups.com
#29458: Documentation incorrectly says Model._meta.get_field() uses related_name
instead of related_query_name
-------------------------------+------------------------------------
Reporter: Tomasz Knapik | Owner: Jeff
Type: Bug | Status: closed
Component: Documentation | Version: 2.1
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"2d6776ffe0b58f729f4372635b49a59da99ca206" 2d6776f]:
{{{
#!CommitTicketReference repository=""
revision="2d6776ffe0b58f729f4372635b49a59da99ca206"
Fixed #29458 -- Doc'd how related_query_name affects
Model._meta.get_field().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29458#comment:6>

Django

unread,
Jun 28, 2018, 8:02:49 PM6/28/18
to django-...@googlegroups.com
#29458: Documentation incorrectly says Model._meta.get_field() uses related_name
instead of related_query_name
-------------------------------+------------------------------------
Reporter: Tomasz Knapik | Owner: Jeff
Type: Bug | Status: closed
Component: Documentation | Version: 2.1
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"ddd9272c2e323b4e8937774425ee93ebebf8eeba" ddd9272c]:
{{{
#!CommitTicketReference repository=""
revision="ddd9272c2e323b4e8937774425ee93ebebf8eeba"
[2.1.x] Fixed #29458 -- Doc'd how related_query_name affects
Model._meta.get_field().

Backport of 2d6776ffe0b58f729f4372635b49a59da99ca206 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29458#comment:7>

Reply all
Reply to author
Forward
0 new messages