1 | """
|
---|
2 | ``db_table`` The name of the table to create for storing the m2m data.
|
---|
3 | If this isn't given, Django will use ``app_label + '_' + table1 + '_' + table2``.
|
---|
4 | """
|
---|
5 |
|
---|
6 | from django.db import models
|
---|
7 |
|
---|
8 | class Publication(models.Model):
|
---|
9 | title = models.CharField(maxlength=30)
|
---|
10 |
|
---|
11 | def __str__(self):
|
---|
12 | return self.title
|
---|
13 |
|
---|
14 | class Meta:
|
---|
15 | ordering = ('title',)
|
---|
16 |
|
---|
17 | class Article(models.Model):
|
---|
18 | headline = models.CharField(maxlength=100)
|
---|
19 | publications = models.ManyToManyField(Publication, db_table='newname')
|
---|
20 |
|
---|
21 | def __str__(self):
|
---|
22 | return self.headline
|
---|
23 |
|
---|
24 | class Meta:
|
---|
25 | ordering = ('headline',)
|
---|
26 |
|
---|
27 | __test__ = {'API_TESTS':"""
|
---|
28 | # Create a couple of Publications.
|
---|
29 | >>> p1 = Publication(id=None, title='The Python Journal')
|
---|
30 | >>> p1.save()
|
---|
31 | >>> p2 = Publication(id=None, title='Science News')
|
---|
32 | >>> p2.save()
|
---|
33 |
|
---|
34 | >>> p1.id
|
---|
35 | 1
|
---|
36 |
|
---|
37 | # Should be able to query all of the Publications
|
---|
38 | >>> Publication.objects.all()
|
---|
39 | [<Publication: Science News>, <Publication: The Python Journal>]
|
---|
40 |
|
---|
41 | # Create a couple of Articles.
|
---|
42 | >>> a1 = Article(id=None, headline='Django lets you build Web apps easily')
|
---|
43 | >>> a1.save()
|
---|
44 |
|
---|
45 | >> a1.id
|
---|
46 | 1
|
---|
47 |
|
---|
48 | # Associate the Article with some Publications.
|
---|
49 | >>> a1.publications.add(p1)
|
---|
50 | >>> a1.publications.add(p2)
|
---|
51 |
|
---|
52 | # Should be able to get all of the related publications
|
---|
53 | >>> a1.publications.all()
|
---|
54 | [<Publication: Science News>, <Publication: The Python Journal>]
|
---|
55 |
|
---|
56 | >>> Article.objects.filter(headline__exact='Django lets you build Web apps easily')
|
---|
57 | [<Article: Django lets you build Web apps easily>]
|
---|
58 |
|
---|
59 | # We can perform kwarg queries across m2m relationships
|
---|
60 | >>> Article.objects.filter(publications__id__exact='1')
|
---|
61 | [<Article: Django lets you build Web apps easily>]
|
---|
62 |
|
---|
63 | # Custom SQL should return correct values still
|
---|
64 | >>> Article.objects.extra(where=['id IN (1, 2, 3, 4, 5)'])
|
---|
65 | [<Article: Django lets you build Web apps easily>]
|
---|
66 |
|
---|
67 | # Custom SQL using the old table name should error
|
---|
68 | >>> Article.objects.extra(tables=['custom_tables_article_publications'])
|
---|
69 | Traceback (most recent call last):
|
---|
70 | ...
|
---|
71 | OperationalError: no such table: custom_tables_article_publications
|
---|
72 |
|
---|
73 | # Custom SQL using the correct table name should return correct values still
|
---|
74 | >>> Article.objects.extra(tables=['newname'])
|
---|
75 | [<Article: Django lets you build Web apps easily>, <Article: Django lets you build Web apps easily>]
|
---|
76 |
|
---|
77 | # Changing table name must not give new properties to model
|
---|
78 | >>> a1.newname
|
---|
79 | Traceback (most recent call last):
|
---|
80 | ...
|
---|
81 | AttributeError: 'Article' object has no attribute 'newname'
|
---|
82 |
|
---|
83 | """}
|
---|