[Answered ]-Fetching not with custom primary id in django

1👍

Django will set the primary key of an AutoField or BigAutoField, given that the database supports returning the assigned primary key.

You thus should rewrite the model to:

class Product(models.Model):
    product_id = models.BigAutoField(primary_key=True)
    # …

0👍

The reason for this is that Django does not know what the primary key (pk) of the object is going to be before the object is saved in the database.

That is because Django does not determine the value of the pk for the incoming object, your database does. In order to get the pk, you first have to save the object then retrive its pk.

👤hmaina

Leave a comment