Idea
I would recommend you the same approach that Instragam uses. Their demands seem to follow you.
Generated identifiers should be sorted by time (therefore, the list of photo identifiers for For example, can be sorted without getting more information about the photo) Ideally, identifiers should be 64 bits (for smaller indices and better storage in systems such as Redis). The system should introduce as many new ones as possible. "As far as possible, moving parts are a large part of how we could scale Instagram with very few engineers, choosing simple, easy-to-understand solutions that we trust.
They came up with a system that has 41 bits based on a timestamp, 13 - a shard of the database and 10 - for the auto-enlargement part. It looks like you are not using shards. You can only have 41 bits based on time and 23 bits randomly selected. This leads to an extremely unlikely 1 in 8.3 million chance of conflict if you insert records at the same time. But in practice, you will never come across this. So what about some code:
Identifier Generation
START_TIME = a constant that represents a unix timestamp def make_id(): ''' inspired by http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram ''' t = int(time.time()*1000) - START_TIME u = random.SystemRandom().getrandbits(23) id = (t << 23 ) | u return id def reverse_id(id): t = id >> 23 return t + START_TIME
Note. START_TIME in the above code is some harsh startup time. You can use time.time () * 1000, get the value and set it as START_TIME
Note that the reverse_id method that I posted lets you know at what time the record was created. If you need to track this information, you can do it without adding another field to it! Thus, your primary key actually saves your storage, rather than increasing it.
Model
Now it will look like your model.
class MyClass(models.Model): id = models.BigIntegerField(default = fields.make_id, primary_key=True)
If you make changes to your database outside of django, you will need to create the make_id equivalent as a sql function
How is your leg. This is similar to the approach used by Mongodb to generate _ ID for each object.