#14518: Field.to_python not called on foreign key IDs
-------------------------------------+-------------------------------------
     Reporter:  wolever              |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.2
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |             Triage Stage:  Accepted
     Keywords:                       |      Needs documentation:  0
    Has patch:  0                    |  Patch needs improvement:  0
  Needs tests:  0                    |                    UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------
Changes (by avnimahajan@…):

 * ui_ux:   => 0
 * easy:   => 0


Comment:

 I am also facing same problem. We have changed the default behavior of
 having primary keys. Throughout our code it has been replaced with custom
 UUIDFiled(given below). Till django 1.1 we were not facing any problem but
 while migrating to django1.3, I started facing problem during
 syncdb(duplicate key error while inserting in auth_permissions).
 Since there is change in "django/contrib/auth/management/__init__.py"
 create_permission code. Now it is referring to "content_type" which is a
 Foreign key. In my case its value is not being to_python'd. Thus it fails
 in comparison with a uuid.UUID type value (returned when referred as pk
 which is always to_python'd).

 Please update by when this bug will be solved?


 class UUIDField(models.Field):


     __metaclass__ = models.SubfieldBase
     empty_strings_allowed = False

     def __init__(self, *args, **kwargs):
         if kwargs.get('primary_key', False):
             kwargs['editable'] = False
         kwargs['db_index'] = True
         super(UUIDField, self).__init__(*args, **kwargs)

     def db_type(self, connection):
         return 'uuid'

     def get_internal_type(self):
         return 'UUIDField'

     def to_python(self, value):
         if (value is None) or isinstance(value, uuid.UUID):
             return value
         try:
             return uuid.UUID(value)
         except (AttributeError, TypeError, ValueError):
             return value

     def pre_save(self, obj, add):
         old_val = getattr(obj, self.attname)
         if (self.primary_key and add) and (old_val is None):
             value = uuid.uuid4()
             setattr(obj, self.attname, value)
             return value
         else:
             return old_val

     def get_db_prep_lookup(self, lookup_type, value,connection=None,
 prepared=False):
         if lookup_type == 'exact':
             return [self.get_db_prep_value(value,connection=None,
 prepared=False)]
         elif lookup_type == 'in':
             return [self.get_db_prep_value(v,connection=None,
 prepared=False) for v in value]
         elif lookup_type == 'isnull':
             return []
         raise TypeError(u"Field has invalid lookup type: %s" %
 lookup_type)

     def value_to_string(self, obj):
         value = getattr(obj, self.attname)
         if value is None:
             return value
         else:
             return unicode(value)

     def formfield(self, **kwargs):
         defaults = {
             'form_class': forms.RegexField,
             'regex': UUID_REGEX,
             'max_length': 47,
             'error_messages': {'invalid': u"Enter only valid UUID."}
         }
         defaults.update(kwargs)
         return super(UUIDField, self).formfield(**defaults)

-- 
Ticket URL: <https://code.djangoproject.com/ticket/14518#comment:4>
Django <https://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 django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to