2
17. Custom column/table names
4
If your database column name is different than your model attribute, use the
5
``db_column`` parameter. Note that you'll use the field's name, not its column
8
If your database table name is different than your model name, use the
9
``db_table`` Meta attribute. This has no effect on the API used to
12
If you need to use a table name for a many-to-many relationship that differs
13
from the default generated name, use the ``db_table`` parameter on the
14
``ManyToManyField``. This has no effect on the API for querying the database.
18
from __future__ import unicode_literals
20
from django.db import models
21
from django.utils.encoding import python_2_unicode_compatible
24
@python_2_unicode_compatible
25
class Author(models.Model):
26
first_name = models.CharField(max_length=30, db_column='firstname')
27
last_name = models.CharField(max_length=30, db_column='last')
30
return '%s %s' % (self.first_name, self.last_name)
33
db_table = 'my_author_table'
34
ordering = ('last_name','first_name')
36
@python_2_unicode_compatible
37
class Article(models.Model):
38
headline = models.CharField(max_length=100)
39
authors = models.ManyToManyField(Author, db_table='my_m2m_table')
45
ordering = ('headline',)