[Answered ]-Python Django models id increment by 2

1👍

according to this answer you should override the save() method of your model :

from django.db.models import Max, F
class Role(models.Model):
    id = models.AutoField(primary_key=True) 
    uuid = models.CharField(max_length=36, unique=True)

    def save(self, *args, **kwargs):
        if not self.pk:
            max = Role.objects.aggregate(max=Max(F('id')))['max']
            self.id = max + 2 if max else 1 
        super().save(*args, **kwargs)

try this. maybe helpful.

Leave a comment