By default, Django gives each model the following field:
id = models.AutoField(primary_key=True)
(https://docs.djangoproject.com/en/1.10/topics/db/models/#automatic-primary-key-fields)
This is great and convenient. However, I would like to know whether it is possible to change the name of the id field to more informative name, e.g., item_id. If this is indeed possible, how can I do that?
EDIT: From the answers I understand that it is impossible to do it without setting the primary key explicitly (which is what I wanted to know).
My model has many classes, and I think that it will be clearer to give more informative field names. Does it really matter?
You just reference the source!
If you’d like to specify a custom primary key, just specify
primary_key=True
on one of your fields. If Django sees you’ve explicitly setField.primary_key
, it won’t add the automaticid
column.
class MyModel(models.Model):
item_id = models.AutoField(primary_key=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With