﻿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
20154	Inconsistent model save behavior when assigning model instances to CharFields	adsva	nobody	"Hi,

When I assign a model instance to a CharField on a model and save the instance, it gets saved as the string representation of the instance on insert, but as the string representation of the instance's pk on update.
{{{
from django.db import models

class Country(models.Model):
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class Address(models.Model):
    country = models.CharField(max_length=100)
}}}
{{{
>>> c = Country.objects.get(name='Sweden')
>>> a = Address.objects.create(country=c)
>>> Address.objects.get(pk=a.pk).country
>>> u'Sweden'
>>> a.save()
>>> Address.objects.get(pk=a.pk).country
>>> '1'
}}}

I do realize I should assign c.name instead if c, but it feels like a bug to treat values differently on insert vs update.

The reason seems to be that `SQLUpdateCompiler.as_sql` preps the value using `val.prepare_database_save` if available, whereas `SQLInsertCompiler.as_sql` always uses `field.get_db_prep_save`.

"	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed		schmilblick Matt	Accepted	0	0	0	0	0	0
