Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can UUIDField be used as default_auto_field in Django 3.2.^?

I have been using UUIDField as my primary key in Django. My project has a hierarchy of models with inherited fields, and at the top, I have the following superclass:

import uuid
from django.db import models

class HasIDMixin(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True, name='id')

After updating to Django 3.2.4, I get the following warning:

WARNINGS:
app_base.MyProject: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
    HINT: Configure the DEFAULT_AUTO_FIELD setting or the AppBaseConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.

Following the advice of the warning, I tried both the DEFAULT_AUTO_FIELD in settings.py and the default_auto_field in the app_config and I get the following error:

ValueError: Primary key 'django.db.models.UUIDField' referred by DEFAULT_AUTO_FIELD must subclass AutoField.

I have seen others approach this problem with a custom child class to both UUIDField and AutoField (https://code.djangoproject.com/ticket/32577) but no working solution has been posted. Is this currently possible in Django 3.2.^? If not should I find a different primary key solution or roll back?

like image 509
Anne Haley Avatar asked Jun 06 '21 16:06

Anne Haley


People also ask

What is UUIDField in Django?

UUIDField is a special field to store universally unique identifiers. It uses Python's UUID class. UUID, Universal Unique Identifier, is a python library that helps in generating random objects of 128 bits as ids.

What is DEFAULT_ Auto_ field in Django?

default_auto_field set to BigAutoField . In a future Django release the default value of DEFAULT_AUTO_FIELD will be changed to BigAutoField . To avoid unwanted migrations in the future, either explicitly set DEFAULT_AUTO_FIELD to AutoField : DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

What is _state in Django?

From the Django source code, _state is an instance variable defined in each Model instance that is an instance of ModelState that is defined as: class ModelState(object): """ A class for storing instance state """ def __init__(self, db=None): self.db = db # If true, uniqueness validation checks will consider this a new ...

What is autofield in Django and how to use it?

This automatically added key is usually AutoField. From Django 3.2 onwards Django allows you to set what type of AutoField will be used for these automatically generated primary key fields by setting DEFAULT_AUTO_FIELD.

What is uuidfield in Django?

UUIDField – Django Models Last Updated : 12 Feb, 2020 UUIDField is a special field to store universally unique identifiers. It uses Python’s UUID class.

What is the default field of a model in Django?

By default, Django gives each model the following field: id = models.AutoField(primary_key=True, **options) This is an auto-incrementing primary key. Even if the model doesn’t have any field, a default field will be created named as id. Django Model AutoField Explanation. Illustration of AutoField using an Example.

How do I create a default primary key field in Django?

From Django 3.2 onwards Django allows you to set what type of AutoField will be used for these automatically generated primary key fields by setting DEFAULT_AUTO_FIELD. Considering you explicitly set a primary key for your model Django will not generate a field for it.


Video Answer


1 Answers

When one does not set a model field to be the primary key for that model Django will automatically add a field to your model named id which will be used as the primary key. This automatically added key is usually AutoField. From Django 3.2 onwards Django allows you to set what type of AutoField will be used for these automatically generated primary key fields by setting DEFAULT_AUTO_FIELD.

Considering you explicitly set a primary key for your model Django will not generate a field for it. But still you should specify DEFAULT_AUTO_FIELD as you might have some model where you don't specify the primary key. You can safely keep your UUIDField in your model as it is and also set DEFAULT_AUTO_FIELD like so:

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField' # Or one of the other options as per your choice
like image 74
Abdul Aziz Barkat Avatar answered Oct 27 '22 09:10

Abdul Aziz Barkat