#29900: ForeignKey foo with to_field=bar option: Accessor should be called 
foo_bar
not foo_id
-------------------------------------+-------------------------------------
     Reporter:  Paul Zeinlinger      |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  2.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  to_field             |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 I agree that it could be more intuitive to use
 `f'{field.name}_{field.target_field.name}'` as attribute name. However
 changing it at this point would break backward compatibility for a large
 number of projects and require a deprecation period.

 Also in the cases where it's the referenced primary key name that declares
 a different name the attribute name would be less obvious because of the
 implicit `to_field`. The problematic is even worse when the referenced
 model is declared in another app.

 {{{#!python
 # app1/model.py
 class User(models.Model):
     username = models.CharField(primary_key=True)

 # app2/model.py
 class Message(models.Model):
     user = models.ForeignKey(User)  # Implicit to_field='username'

 Message.objects.create(user_id='foo')  # Would be a TypeError
 Message.objects.create(user_username='foo')
 }}}

 I find it kind of practical that foreign key attribute names are simply
 `f'{field.name}_id'` independently of the referenced field name. In a
 sense it's kind of how `pk` is an alias for whatever the primary key name
 is. That kind of defeats the purity argument IMO.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29900#comment:1>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.f496a14aafccb31581d786e08a3ae0f5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to