Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!