#28856: GenericForeignKey attributes create new instances on every access
--------------------------------------+------------------------------------
     Reporter:  Morgan Wahl           |                    Owner:  nobody
         Type:  Bug                   |                   Status:  new
    Component:  contrib.contenttypes  |                  Version:  1.10
     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 Simon Charette):

 * has_patch:  0 => 1
 * stage:  Unreviewed => Accepted


Comment:

 Thanks Morgan, I missed that! This is quite a complex scenario: GFK, PK
 type mismatch, and MTI so I'm not surprised it broke given the small
 number of tests for PK type coercion.

 The culprit seems to be that related fields don't delegate `to_python` to
 the field they reference so the `OtherSub` primary key, a `OneToOneField`,
 uses the default `Field.to_python` which is an identity function. This
 causes
 
[https://github.com/django/django/blob/b9f8635f58ad743995cad2081b3dc395e55761e5/django/contrib/contenttypes/fields.py#L234
 this line] to be falsey because `rel_obj._meta.pk.to_python('1')` returns
 `'1'` instead of `1`.

 I feel like the correct way to solve that would to implement
 `ForeignKey.to_python` (which `OneToOneField` subclasses) to use
 `self.foreign_related_fields[0].to_python` but even if this causes a
 single failure in the test suite I feel like this would be to intrusive
 for a backport. What I'd favor doing instead is implementing this approach
 in master and adapt `GenericForeignKey.__get__` to work around the issue
 if a backport is deemed eligible.

 Here's a PR for master: https://github.com/django/django/pull/9395
 And one for 1.11.x if you judge it should be backported Tim:
 https://github.com/django/django/pull/9396

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28856#comment:5>
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/068.3a9391d619868539b6391d97f3dbeae4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to