#14518: Field.to_python not called on foreign key IDs
-------------------------------------+-------------------------------------
     Reporter:  wolever              |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |             Triage Stage:  Accepted
     Keywords:                       |      Needs documentation:  0
    Has patch:  0                    |  Patch needs improvement:  0
  Needs tests:  0                    |                    UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------

Comment (by Harm Geerts <hgeerts@…>):

 The reason the first example works is because in
 Bar.objects.create(foo=m1) the m1 Foo instance is cached on the Bar
 instance, so m2.foo.field does not perform a database query.

 The second example fails because m2 has to perform a database lookup, and
 that's where things get fun.

 Your MyField.to_python is called multiple times so the value stored in the
 database is actually "Value: Value: anubhavjoshi".
 When you access bar.foo the primary key value "Value: Value: anubhavjoshi"
 is passed through to_python again and becomes "Value: Value: Value:
 anubhavjoshi" and that primary key does not exist.

 The reason your patch works is because you only had one Foo record in your
 dataset.
 If you had multiple Foo records your test would raise
 MultipleObjectsReturned.
 The only thing the patch does is disable related lookup filters which is
 not what you want ;)

 It may be an idea to add a note to the documentation about custom field
 classes.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/14518#comment:11>
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/065.9e3930251fa4363408a4564bdb2d1fdb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to