home / django_tickets / tickets

tickets: 8633

This data as json

id created changetime last_pulled_from_trac stage status component type severity version resolution summary description owner reporter keywords easy has_patch needs_better_patch needs_tests needs_docs ui_ux
8633 2008-08-28 09:40:42 2012-07-14 17:57:55 2022-03-06 03:42:46.135418 Unreviewed closed Database layer (models, ORM) Uncategorized Normal dev invalid Field default value force to unicode when create custom field. 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 "" }}} nobody flytwokites   0 0 0 0 0 0
Powered by Datasette · Queries took 0.771ms