Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

django change id to uuid

4

this can be done by using a custom base abstract model,using the following steps.

First create a folder in your project call it basemodel then add a abstractmodelbase.py with the following below:
from django.db import models
import uuid


class BaseAbstractModel(models.Model):

    """
     This model defines base models that implements common fields like:
     created_at
     updated_at
     is_deleted
    """
    id = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True, editable=False)
    is_deleted = models.BooleanField(default=False)

    def soft_delete(self):
        """soft  delete a model instance"""
        self.is_deleted=True
        self.save()

    class Meta:
        abstract = True
        ordering = ['-created_at']
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #django #change #id #uuid
ADD COMMENT
Topic
Name
2+4 =