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?
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.
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'
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 ...
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.
UUIDField – Django Models Last Updated : 12 Feb, 2020 UUIDField is a special field to store universally unique identifiers. It uses Python’s UUID class.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With