django model UUID field

  1. Import the necessary modules:
from django.db import models
import uuid
  1. Define a Django model with a UUID field:
class YourModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    # Add other fields as needed
  1. Explanation of the model fields:
  2. id: This is the primary key field for the model, and it is set as a UUID field.
  3. models.UUIDField: This indicates that the field will store a UUID.
  4. primary_key=True: Specifies that this field is the primary key for the model.
  5. default=uuid.uuid4: Sets the default value for the field to a randomly generated UUID using the uuid4 function.
  6. editable=False: Prevents the field from being edited through forms or the Django admin site.

  7. Add any additional fields to the model as needed for your application.

  8. Run migrations to apply the changes to the database:

python manage.py makemigrations
python manage.py migrate