Id field in Model Django
Ofcourse, you can.
city_id = models.BigIntegerField(primary_key=True)
By default, Django gives each model the following field:
id = models.AutoField(primary_key=True)
This is an auto-incrementing primary key.
So for your case is:
city_id = models.AutoField(primary_key=True)
The answer is YES,
Something like this:
city_id = models.PositiveIntegerField(primary_key=True)
Here, you are overriding the id
. Documentation here
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 set Field.primary_key, it won’t add the automatic id column.
Alternatively, You can always define a model property and use that . Example
class City(models.Model)
#attributes
@property
def city_id(self):
return self.id
and access it as city.city_id
where you would normally do city.id
city_id = models.AutoField(primary_key=True)