1๐
โ
If you define a field of a model as an AutoField
it will do this exact thing
EDIT:
If you want to define a custom auto incrementing ID you can do it like:
from django.db import models
class MyModel(models.Model):
_id_counter = 0
custom_id = models.IntegerField()
@classmethod
def create(cls):
my_model = cls(custom_id=MyModel._id_counter)
MyModel._id_counter += 1
return my_model
model1 = MyModel.create()
๐คKostas Pelelis
Source:stackexchange.com