0

I am adding a field to a database:

identifier = models.UUIDField(unique=True, null=True, blank=True)

but I am curious if this will behave like a CharField or a number field when someone saves an instance in the admin without a value. Will it try to save the value as an empty string "" or will it be smart and save it as null? I am using Postgres for the database. I've seen other answers for CharField, but nothing that speaks to UUIDField.

My end goal is to have a field that is guaranteed to be unique, and will default to null. would it be more appropriate to write it as:

identifier = models.UUIDField(unique=True, null=True, default=None) or will the admin form validation complain if no value is provided?

Matt
  • 5,028
  • 2
  • 28
  • 55

1 Answers1

4

The Django documentation doesn't specify how UUIDField handles null values.

However, a look at the source code shows that it's not a subset of CharField, and that it has explicit support for handling null values.

So it appears that the field will indeed store None values as NULL, and since such values are always unique on PostgreSQL your approach should work on that database.

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102