Debian Codegraph @

tests/custom_columns/models.py

raw
1 """
2 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 django.db import models
19
20
21 class Author(models.Model):
22 Author_ID = models.AutoField(primary_key=True, db_column="Author ID")
23 first_name = models.CharField(max_length=30, db_column="firstname")
24 last_name = models.CharField(max_length=30, db_column="last")
25
26 class Meta:
27 db_table = "my_author_table"
28 ordering = ("last_name", "first_name")
29
30 def __str__(self):
31 return "%s %s" % (self.first_name, self.last_name)
32
33
34 class Article(models.Model):
35 Article_ID = models.AutoField(primary_key=True, db_column="Article ID")
36 headline = models.CharField(max_length=100)
37 authors = models.ManyToManyField(Author, db_table="my_m2m_table")
38 primary_author = models.ForeignKey(
39 Author,
40 models.SET_NULL,
41 db_column="Author ID",
42 related_name="primary_set",
43 null=True,
44 )
45
46 class Meta:
47 ordering = ("headline",)
48
49 def __str__(self):
50 return self.headline