Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set auto increment initial value for id in django model

i have a model with just one field. 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 field in SQL.

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

Is there any way to do this in Django?

like image 491
A.J. Avatar asked May 21 '13 06:05

A.J.


1 Answers

I know this question is old, but I'm posting this method for those who find this question in search results and need a good solution for more recent Django versions (i.e. Django >= 1.8):

Create a data migration as documented here. This solution will run when you migrate your app (and, probably, as part of your deployment strategy) and allows you to write code that caters to the specific needs of your database.

I consider this the cleanest method of setting an initial value for auto-incrementing database fields in Django >= 1.7 because it takes advantage of standard Django migrations and doesn't hijack other features of the framework in ways that may not work in the future.

like image 74
Adam Avatar answered Oct 19 '22 03:10

Adam