#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/048.1eabd56565c7be6076a443da540f9dcb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to