Django 1.10 UUIDField returns either string or UUID

When upgrading from Django 1.9.13 to Django 1.10.7, I got a weird problem with Django's native UUIDField.

We use this UUIDField in our custom user model, for example:

username = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

      

In 1.9, this always returns an instance of the UUID. In 1.10, this returns a string when instantiating a new model.

Compare the following test cases:

1.9.13

>>> u = User.objects.last()
>>> u2 = UserFactory()
>>> u3 = User.objects.create()
>>> u.pk
UUID('e7e0f87d-1ed4-4293-829f-b0b745ccd676')
>>> u2.pk
UUID('f8e9a4a9-2265-4cd7-9813-00ffe7fd922a')
>>> u3.pk
UUID('0cb736d7-f8a0-4057-9c89-44fa114f4f82')

      

1.10.7

>>> u = User.objects.last()
>>> u2 = UserFactory()
>>> u3 = User.objects.create()
>>> u.pk
UUID('e7e0f87d-1ed4-4293-829f-b0b745ccd676')
>>> u2.pk
'f8e9a4a9-2265-4cd7-9813-00ffe7fd922a'
>>> u3.pk
'0cb736d7-f8a0-4057-9c89-44fa114f4f82'

      

This difference gives problems with different unittests. I can work around this by forcing both lines, but I want to understand why the UUIDField behaves the way it feels inconsistent.

+3


source to share


1 answer


The problem is caused by a change in behavior in the Django AbstractBaseUser class. The class got a clean method that I called to save. Inside the new clean method, the normalize_username method was called which made the username be text.



To avoid supercalls in AbstractBaseUser, we no longer normalize the username, which we don't want as our username field is a UUID.

0


source







All Articles