Home > Software design >  Is this the correct way of making primary keys in django?
Is this the correct way of making primary keys in django?

Time:04-30

class profiles(models.model):
    customer_ID = models.IntegerField().primary_key

Is this the correct way of making a primary key in django??

CodePudding user response:

The correct syntax for primary key is-:

class profiles(models.model):
    customer_ID = models.IntegerField(primary_key=True)

CodePudding user response:

Is this the correct way of making a primary key in django??

No. You use an AutoField [Django-doc] for a primary key, since then the values are dispatched by the database, so:

class profiles(models.model):
    customer_ID = models.AutoField(primary_key=True, editable=False)

But you do not have to specify a primary key: if you do not specify one yourself, Django will add one with the name id to the model automatically.

  • Related