#14518: Field.to_python not called on foreign key IDs -------------------------------------+------------------------------------- Reporter: wolever | Owner: nobody Type: Bug | Status: new Component: Database layer | Version: 1.2 (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 anubhav9042): Well there have been some changes in the code and some things have changed. Here is my models.py {{{ from django.db import models class MyField(models.CharField): __metaclass__ = models.SubfieldBase def __init__(self, *args, **kwargs): super(MyField, self).__init__(*args, **kwargs) def to_python(self, value): return "Value: %s" % value class Foo(models.Model): field = MyField(max_length=30, primary_key=True) class Bar(models.Model): foo = models.ForeignKey(Foo) }}} In shell, I get correct values here: {{{ >>> m1 = Foo.objects.create(field="anubhavjoshi") >>> m2 = Bar.objects.create(foo=m1) >>> m2.foo_id u'Value: Value: anubhavjoshi' >>> m2.foo.field u'Value: Value: anubhavjoshi' }}} But if I do: {{{ >>> m1 = Foo.objects.create(field="anubhavjoshi") >>> m2 = Bar.objects.create(foo=m1) >>> m2 = Bar.objects.all()[0] >>> m2.foo_id u'Value: Value: anubhavjoshi' >>> m2.foo.field DoesNotExist: Foo matching query does not exist. >>> m2.foo DoesNotExist: Foo matching query does not exist. }}} Following traceback: {{{ --------------------------------------------------------------------------- DoesNotExist Traceback (most recent call last) C:\Users\Anubhav\DjangoDev\apps\<ipython-input-7-3a4a21c18010> in <module>() ----> 1 m2.foo c:\users\anubhav\django\django\db\models\fields\related.pyc in __get__(self, instance, instance_type) 562 qs = qs.filter(extra_filter, **params) 563 # Assuming the database enforces foreign keys, this won't fail. --> 564 rel_obj = qs.get() 565 if not self.field.rel.multiple: 566 setattr(rel_obj, self.field.related.get_cache_name(), instance) c:\users\anubhav\django\django\db\models\query.pyc in get(self, *args, **kwargs) 374 raise self.model.DoesNotExist( 375 "%s matching query does not exist." % --> 376 self.model._meta.object_name) 377 raise self.model.MultipleObjectsReturned( 378 "get() returned more than one %s -- it returned %s!" % ( }}} -- Ticket URL: <https://code.djangoproject.com/ticket/14518#comment:7> 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/065.a8e14afad37a7bf1825708bbd711456f%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.