Debian Codegraph @

tests/model_options/models/tablespaces.py

raw
1 from django.db import models
2
3 # Since the test database doesn't have tablespaces, it's impossible for Django
4 # to create the tables for models where db_tablespace is set. To avoid this
5 # problem, we mark the models as unmanaged, and temporarily revert them to
6 # managed during each test. We also set them to use the same tables as the
7 # "reference" models to avoid errors when other tests run 'migrate'
8 # (proxy_models_inheritance does).
9
10
11 class ScientistRef(models.Model):
12 name = models.CharField(max_length=50)
13
14
15 class ArticleRef(models.Model):
16 title = models.CharField(max_length=50, unique=True)
17 code = models.CharField(max_length=50, unique=True)
18 authors = models.ManyToManyField(ScientistRef, related_name="articles_written_set")
19 reviewers = models.ManyToManyField(
20 ScientistRef, related_name="articles_reviewed_set"
21 )
22
23
24 class Scientist(models.Model):
25 name = models.CharField(max_length=50)
26
27 class Meta:
28 db_table = "model_options_scientistref"
29 db_tablespace = "tbl_tbsp"
30 managed = False
31
32
33 class Article(models.Model):
34 title = models.CharField(max_length=50, unique=True)
35 code = models.CharField(max_length=50, unique=True, db_tablespace="idx_tbsp")
36 authors = models.ManyToManyField(Scientist, related_name="articles_written_set")
37 reviewers = models.ManyToManyField(
38 Scientist, related_name="articles_reviewed_set", db_tablespace="idx_tbsp"
39 )
40
41 class Meta:
42 db_table = "model_options_articleref"
43 db_tablespace = "tbl_tbsp"
44 managed = False
45
46
47 # Also set the tables for automatically created models
48
49 Authors = Article._meta.get_field("authors").remote_field.through
50 Authors._meta.db_table = "model_options_articleref_authors"
51
52 Reviewers = Article._meta.get_field("reviewers").remote_field.through
53 Reviewers._meta.db_table = "model_options_articleref_reviewers"