26👍
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
- [Django]-Authorization Credentials Stripped — django, elastic beanstalk, oauth
- [Django]-Explicitly set MySQL table storage engine using South and Django
- [Django]-How to filter (or replace) unicode characters that would take more than 3 bytes in UTF-8?
7👍
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)
- [Django]-How do I print out the contents of my settings in a django shell?
- [Django]-Warning: cannot find svn location for distribute==0.6.16dev-r0
- [Django]-Django Multiple Choice Field / Checkbox Select Multiple
- [Django]-Database returned an invalid value in QuerySet.dates()
- [Django]-How to render menu with one active item with DRY?
- [Django]-Django template can't loop defaultdict
Source:stackexchange.com