Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
Django 1.10 UUIDField returns either string or UUID

While upgrading from Django 1.9.13 to Django 1.10.7 I encountered a weird issue with Django's native UUIDField.

We use this UUIDField on our custom User model like:

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

In 1.9 this always returns a UUID instance. In 1.10 this returns a string when creating a new model instance.

Compare the following test examples:

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 issues with various unittests. I can work around it by forcing both to string, but I wish to understand why UUIDField behaves the way it does as it feels inconsistent.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The problem was caused by changed behaviour in Django's AbstractBaseUser class. The class received a clean method which I called on save. Inside the new clean method a normalize_username method was called which forced the username to be text.

By avoiding the super call to AbstractBaseUser we no longer normalize the username which isn't something we wanted anyways as our username field is a UUID.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda