1

I made this django model:

class Car(models.Model):
    car_id = models.AutoField(primary_key=True)
    engine_size = models.FloatField()
    fuel_consumption = models.FloatField()

    def __str__(self):
        return(str(self.car_id))

The automatic field generator in django always starts from 1. How do I make it generate id from initial value=100. For example, 100,101,102...

Is there any built-in tool, or I just need to create a custom function for this?

  • Possible duplicate of [How to get Django AutoFields to start at a higher number](https://stackoverflow.com/questions/117800/how-to-get-django-autofields-to-start-at-a-higher-number) – wfehr Sep 24 '19 at 04:36

1 Answers1

4

You can achieve this by creating empty migration.

python manage.py makemigrations YOUR_APP_NAME --empty

An empty migration will create in-app label and make change like following

from django.db import migrations

class Migration(migrations.Migration):

    dependencies = [

    ]

    operations = [
        migrations.RunSQL('ALTER SEQUENCE APPNAME_CAR_id_seq RESTART WITH 100;')
    ]
shafik
  • 6,098
  • 5
  • 32
  • 50