﻿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
8633	Field default value force to unicode when create custom field.	flytwokites	nobody	"When create a custom field like document ""http://www.djangoproject.com/documentation/custom_model_fields/"" does:
{{{
class Hand(object):
    def __init__(self, north, east, south, west):
        self.north = north
        self.east = east
        self.south = south
        self.west = west

class HandField(...):
    ...
    def to_python(self, value):
        ...

class Foo(models.Model):
    hand = HandField(default=Hand(...))

f = Foo()
# This will use the default hand value, but value passed to HandField.to_python is string format of hand.
# If we not define a Hand.__str__ function, this string is like '<Hand instance at 0x????????>'.
# So the HandField.to_python not able to recognize this string value.
# So we must define a __str__ on Hand, and HandField must convert this string back to Hand instance.
# In some custom field value __str__ output is a human readable text, and difficult to convert back to a instance.
# So it is better to pass default value direct to to_python().
f.save()
}}}

The code in django/db/models/fields/__init__.py
{{{
class Field:
    def get_default(self):
        ""Returns the default value for this field.""
        if self.default is not NOT_PROVIDED:
            if callable(self.default):
                return self.default()
            return force_unicode(self.default, strings_only=True)
        if not self.empty_strings_allowed or (self.null and not connection.features.interprets_empty_strings_as_nulls):
            return None
        return """"
}}}
"	Uncategorized	closed	Database layer (models, ORM)	dev	Normal	invalid			Unreviewed	0	0	0	0	0	0
