primary key auto increment python django 1

primary key auto increment python django

In Django

1 : we have default field with name "id" which is auto increment.
2 : You can define a auto increment field using AutoField field as below.

class Order(models.Model):
    auto_increment_id = models.AutoField(primary_key=True)
    #you use primary_key = True if you do not want to use default field "id" given by django to your model

Here is what the above code is Doing:
1. It’s creating a new model called Order.
2. It’s creating a new field called auto_increment_id.
3. It’s setting the primary_key=True, which means that this field will be the primary key for the model.
4. It’s setting the field type to AutoField, which means that it will automatically increment.

Similar Posts