PageRenderTime 137ms CodeModel.GetById 132ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/custom_columns/models.py

https://code.google.com/p/mango-py/
Python | 40 lines | 36 code | 0 blank | 4 comment | 0 complexity | a68eb03134eee29b13a5d38cdd260e9f MD5 | raw file
 1"""
 217. Custom column/table names
 3
 4If 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
 6name, in API usage.
 7
 8If 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
10query the database.
11
12If you need to use a table name for a many-to-many relationship that differs
13from the default generated name, use the ``db_table`` parameter on the
14``ManyToManyField``. This has no effect on the API for querying the database.
15
16"""
17
18from django.db import models
19
20class Author(models.Model):
21    first_name = models.CharField(max_length=30, db_column='firstname')
22    last_name = models.CharField(max_length=30, db_column='last')
23
24    def __unicode__(self):
25        return u'%s %s' % (self.first_name, self.last_name)
26
27    class Meta:
28        db_table = 'my_author_table'
29        ordering = ('last_name','first_name')
30
31class Article(models.Model):
32    headline = models.CharField(max_length=100)
33    authors = models.ManyToManyField(Author, db_table='my_m2m_table')
34
35    def __unicode__(self):
36        return self.headline
37
38    class Meta:
39        ordering = ('headline',)
40