Set initial auto increment value for id in django model

I have a single field model. name.

class City(models.Model): <br> name = models.CharField(max_length=30) 

Django sets id by itself, so I end up with id and a description in SQL.

but the id field is always incremented by 1. I want to start it from 1000, and then increment by one.

Is there a way to do this in Django?

+4
source share
2 answers

You can use the initial data to give your model IDs from 1000. I think that if the first PK models are 1000 django, they will continue from 1001.

Or you create a custom_id attribute that is always 1000 + id. So you have an extra field in your database, but it will work.

0
source

I know this question is old, but I am sending this method to those who find this question in the search results and need a good solution for later versions of Django (i.e. Django> = 1.8):

Create the data transfer as described here . This decision will be executed when the application is ported (and, possibly, as part of a deployment strategy) and allows you to write code that meets the specific needs of your database.

I consider this the cleanest method for setting the initial value for fields with the automatic nature of the database in Django> = 1.7, since it uses standard Django migrations and does not capture other structure functions in ways that may not work in the future.

+3
source

Source: https://habr.com/ru/post/1481882/


All Articles