[Solved]-How to get the primary key id that is auto generated in Django Models

19👍

You can skip defining the id column altogether, ie:

class StudentProfile(models.Model):
    # id=models.IntegerField(primary_key=True)
    joining_on=models.DateField()
    brothername=models.CharField(max_length=45)

    class Meta:
        db_table="student_profile"

Django will create or use an autoincrement column named id by default, which is the same as your legacy column.

If the column name was different, you could use an AutoField with a different name, such as:

my_id = models.AutoField(primary_key=True)
👤Selcuk

Leave a comment