Primary Key AutoField -> UUID Field

67 views
Skip to first unread message

Brian Carter

unread,
Nov 14, 2020, 11:32:38 AM11/14/20
to django-d...@googlegroups.com
Feature Request:

I like to use the UUIDField as the primary key in my models, and I think it would be nice to have django expose a setting in the settings.py to override the default primary key on models.

Currently, if I don’t specify a field as the primary key, Django automatically uses an AutoField (auto-incrementing integer). I’d like to be able to automatically use a UUID field, but then still be overridden in a specific model if I specify a different primary key.

Brian Carter
brianrc...@gmail.com

אורי

unread,
Nov 14, 2020, 11:55:01 AM11/14/20
to Django developers (Contributions to Django itself)
Hi Brian,

In Speedy Net we use a randomly generated string of digits, either 15 digits or 20 digits, as a primary key in all our models. We never use auto-incrementing integers as primary keys. You can take a look at our implementation, especially class BaseModel which is the base class for all our models, and class TimeStampedModel (most of our models inherit from TimeStampedModel):


We also defined managers with querysets in which bulk_create() and delete() are disabled.

Uri Rodberg, Speedy Net.


--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/EAF4690E-C862-4C68-A7DD-C2979F19BC3C%40gmail.com.

Tom Forbes

unread,
Nov 14, 2020, 1:37:44 PM11/14/20
to django-d...@googlegroups.com
Uri, I would definitely suggest using an actual UUID rather than rolling your own ad-hoc UUID implementation, even if your database does not support a native UUID type.

Brian, there is some work ongoing that should make this possible. We will add a setting to control the type of the auto-created primary key type. Initially it will be restricted to AutoField subclasses only, but the intent is to make UUIDFields work with this as well. 

The first bit of work is here: https://github.com/django/django/pull/13179

Tom

On 14 Nov 2020, at 16:54, אורי <u...@speedy.net> wrote:



Tim Graham

unread,
Nov 14, 2020, 5:47:28 PM11/14/20
to Django developers (Contributions to Django itself)
Hi Brian, There are existing threads about that (try searching the archives for 'default pk' to find some of them). Here's a PR that adds the setting you described: https://github.com/django/django/pull/13179. Perhaps you would like to try it out and see if it meets your needs.

David Nugent

unread,
Nov 14, 2020, 6:44:38 PM11/14/20
to django-d...@googlegroups.com


On 20201115, at 09:47, Tim Graham <timog...@gmail.com> wrote:

Hi Brian, There are existing threads about that (try searching the archives for 'default pk' to find some of them). Here's a PR that adds the setting you described: https://github.com/django/django/pull/13179. Perhaps you would like to try it out and see if it meets your needs.

Agree - there are aspects of a UUID that avoid collisions with a far lesser probability than an arbitrary random value.

I use this:

from django.db import models
import uuid


class SomeModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    ...

As already mentioned, using abstract models for this is also useful, helps with DRY.

/d


Reply all
Reply to author
Forward
0 new messages