简体   繁体   中英

Django model attribute and database field with different name in model

I have a database table called Person contains following columns:

 Id,
 first_name,
 last_name,

So is there any way to assign different name to table fields in django model. like this

class Person(models.Model):
firstname = models.CharField(max_length = 30)
lastname = models.CharField(max_length = 30)

firstname instead of first_name

and

lastname instead of last_name

You can pass db_column to the field to customise the column name for a field

class Person(models.Model):
    firstname = models.CharField(max_length=30, db_column='first_name')
    lastname = models.CharField(max_length=30, db_column='last_name')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM