﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29173	Document that Model.save() doesn't refresh fields from the database	Xtreak	nobody	"When there is a custom field in the model and we do some transformation before storing it to the database. Hence while creating the object we give a value which is transformed and stored. But the returned object as a result of the create call has the given value instead of the transformed value.

In the below model I have a custom field FooField which always returns ""foo"" as the value to be stored but when I create an object with another value say 'a' it only has the value 'a' stored in the returned object. I need to do a refresh_from_db to clear the wrong value and to fetch the correct value. I couldn't find this documented at [https://docs.djangoproject.com/en/2.0/howto/custom-model-fields/]. If this an expected behavior adding a note in the documentation will be of help.

models.py

{{{
from __future__ import unicode_literals

from django.db import models

# Create your models here.

class FooField(models.CharField):

    def to_python(self, value):
        value = super(self.__class__, self).to_python(value)
        return ""foo""

    def get_prep_value(self, value):
        value = super(self.__class__, self).get_prep_value(value)
        return ""foo""

class FooBase(models.Model):

    base_field = models.CharField(max_length=120)

class Foo(FooBase):

    custom_field = FooField(max_length=120)

}}}

Shell session 

{{{
In [11]: bar = Foo.objects.create(base_field='1', custom_field='a')

In [12]: bar.custom_field
Out[12]: 'a'

In [13]: bar.refresh_from_db()

In [14]: bar.custom_field
Out[14]: 'foo'

In [15]: bar.id
Out[15]: 10
}}}

{{{
sqlite> select * from TestApp_foo;
10|foo
}}}"	Cleanup/optimization	closed	Documentation	2.0	Normal	invalid			Accepted	0	0	0	0	0	0
