#8633: Field default value force to unicode when create custom field.
------------------------------+---------------------------------------------
 Reporter:  flytwokites       |       Owner:  nobody    
   Status:  new               |   Milestone:            
Component:  Database wrapper  |     Version:  SVN       
 Keywords:                    |       Stage:  Unreviewed
Has_patch:  0                 |  
------------------------------+---------------------------------------------
 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 ""
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/8633>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to