fetching not with custom primary id in django
I have created a table in which the primary id have to customize id product_id like
class Product(models.Model):
product_id = models.BigIntegerField(auto_created = True,primary_key = True, unique=True)
name = models.CharField(max_length=200)
ref = models.CharField(max_length=100)
number= models.CharField(max_length=100)
class Meta:
db_table = "products"
def __str__(self):
return self.name
after creating the record I want to get the id of the latest record but when I retrieve the data with this id getting None
product = Product.objects.create(name=name, ref=ref, number=number)
print(product.product_id)
product.product_id
id getting null
Pleae give me a solution to why this is happening.
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)
# …