I originally asked this on IRC.  It was suggested to raise the question in 
the mailing list. I've copied the log below with irrelevant lines 
removed (dpastes provided after the quoted irc)

<gp> Why is from_db_value not called on Cls.objects.create()?  For example, 
> in the CashModel test, why isn't it tested that cash is an instance of Cash 
> after create? 

<gp> 
> https://github.com/django/django/blob/master/tests/from_db_value/tests.py

<gp> Something like http://dpaste.com/1R69S5F  I seem to be getting the 
> wrong value for a custom primary key in the admin because of this

<gp> *only on the create redirect

<gp> Or is this expected to be handled by the field?

<timograham> gp: this seems similar to #24028

<gp> timograhm: would that test 'test_create()' in the dpaste fit in with 
> the from_db_value?  Or should the create test be somewhere else?

<timograham> seems okay there, does it pass or are you proposing a behavior 
> change?

<gp> When I was trying to figure out why my values were incorrect I tried 
> that test and it failed.  But I would need to verify in a clean project 
> before sending a pr

<gp> but I think the actual fix is outside of my knowledge

<gp> This ugly hack seems to fix it on my field if that means anything to 
> you http://dpaste.com/3J0C5DD

<timograham> gp: it seems like it could be a useful behavior. I guess it 
> has probably been discussed before but I'm not aware of the discussion. 
> Maybe it would be worth raising on the mailing list and at least 
> documenting the reasons for the current behavior if it can't be changed due 
> to backwards compatibility.


 The first dpaste (1R69S5F) is a copy of the from_db_value/tests.py with 
the following modifications:

class FromDBValueTest(TestCase):
    def setUp(self):
        self.obj = CashModel.objects.create(cash='12.50')

    def test_create(self):
        self.assertIsInstance(self.obj.cash, Cash)


The second dpaste (3J0C5DD) is the following:

    def contribute_to_class(self, cls, name):
        super(IntegerIdentifierBase, self).contribute_to_class(cls, name)
        cls_save = cls.save
        def save_wrapper(obj, *args, **kwargs):
            cls_save(obj, *args, **kwargs)
            value = getattr(obj, self.attname)
            value = self.to_python(value)
            setattr(obj, self.attname, value)
        cls.save = save_wrapper



-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/3df06313-41a4-42ab-8fc6-f1cdeee7b515%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to