0

I would like to make Django generate 6+ digits number id's for one Model. I don't want to start from zero but I want this id's to be clear and readable by users. So the good one is for example: 658975

How to do that?

I've tried this:

class MyUUIDModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

But uuid generates huge sequences which is not user-friendly.

Do you have any advices? Maybe setting minimum number of autoincrement pk would be enough.

Milano
  • 18,048
  • 37
  • 153
  • 353

2 Answers2

0

Can't you simply use pk+random number to show to the user and keeping the rest of the logic same ?

Else here you go How to make a primary key start from 1000?

Write a one of migration for this.

Community
  • 1
  • 1
kawadhiya21
  • 2,458
  • 21
  • 34
  • This is not a bad idea... but it's too much refactoring/rewriting code. For example get requests with id etc... too vulnerable to making mistakes in code. But if nothing else, I would try this. – Milano Jul 06 '16 at 11:03
0

The best options to set the primary key to start from 100000. For example(MySQL),

ALTER TABLE django_app_table_name AUTO_INCREMENT = 100000

Since these commands are depends on the database vendor, it is better to write a our own custom migration using the Django migration documentation.

Siva Arunachalam
  • 7,582
  • 15
  • 79
  • 132