What I think could work is using an IntegerField
(pretty much what an AutoField
uses under the hood), and increment that on the model's first save (before it's ever put into the database).
I wrote an example model to show this below.
from django.db import models
class MyModel(models.Model):
# This is what you would increment on save
# Default this to one as a starting point
display_id = models.IntegerField(default=1)
# Rest of your model data
def save(self, *args, **kwargs):
# This means that the model isn't saved to the database yet
if self._state.adding:
# Get the maximum display_id value from the database
last_id = self.objects.all().aggregate(largest=models.Max('display_id'))['largest']
# aggregate can return None! Check it first.
# If it isn't none, just use the last ID specified (which should be the greatest) and add one to it
if last_id is not None:
self.display_id = last_id + 1
super(MyModel, self).save(*args, **kwargs)
This, in theory, just replicates what AutoField
does, just with a different model field.