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.
- [Answered ]-In django-rest-framework, how can I have a resource that is only editable by the user that created it?
- [Answered ]-Django: Passing a queryset to another view with HttpResponseRedirect
- [Answered ]-Input_formats in django admin has no effect
Source:stackexchange.com