Rushikesh Sabde
Rushikesh Sabde

Reputation: 1626

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

Upvotes: 7

Views: 2531

Answers (1)

Iain Shelvington
Iain Shelvington

Reputation: 32304

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

Upvotes: 12

Related Questions