~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/custom_columns/models.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
17. Custom column/table names
 
3
 
 
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
 
6
name, in API usage.
 
7
 
 
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
 
10
query the database.
 
11
 
 
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.
 
15
 
 
16
"""
 
17
 
 
18
from __future__ import unicode_literals
 
19
 
 
20
from django.db import models
 
21
from django.utils.encoding import python_2_unicode_compatible
 
22
 
 
23
 
 
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')
 
28
 
 
29
    def __str__(self):
 
30
        return '%s %s' % (self.first_name, self.last_name)
 
31
 
 
32
    class Meta:
 
33
        db_table = 'my_author_table'
 
34
        ordering = ('last_name','first_name')
 
35
 
 
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')
 
40
 
 
41
    def __str__(self):
 
42
        return self.headline
 
43
 
 
44
    class Meta:
 
45
        ordering = ('headline',)
 
46