Django model attribute and database field with different name in model

Viewed 1101

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

1 Answers

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')
Related