Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3098 -- Added db_table parameter to m2m fields, allowing the s…

…pecification of a custom table name for the m2m table. Thanks, Wolfram Kriesing.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4429 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 54feaca70f77b39093fe417dcd89eb7c74930e2e 1 parent 12ad69c
Russell Keith-Magee authored January 25, 2007
6  django/db/models/fields/related.py
@@ -629,6 +629,7 @@ def __init__(self, to, **kwargs):
@@ -651,7 +652,10 @@ def get_choices_default(self):
4  docs/model-api.txt
@@ -874,6 +874,10 @@ the relationship should work. All are optional:
874 874
                              force Django to add the descriptor for the reverse
875 875
                              relationship, allowing ``ManyToMany`` relationships to be
876 876
                              non-symmetrical.
  877
+			     
  878
+    ``db_table``             The name of the table to create for storing the many-to-many 
  879
+                             data. If this is not provided, Django will assume a default
  880
+                             name based upon the names of the two tables being joined.
877 881
 
878 882
     =======================  ============================================================
879 883
 
92  tests/modeltests/custom_columns/models.py
... ...
@@ -1,53 +1,105 @@
1 1
 """
2  
-17. Custom column names
  2
+17. Custom column/table names
3 3
 
4 4
 If your database column name is different than your model attribute, use the
5 5
 ``db_column`` parameter. Note that you'll use the field's name, not its column
6 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
+ManyToMany field. This has no effect on the API for querying the database.
  15
+
7 16
 """
8 17
 
9 18
 from django.db import models
10 19
 
11  
-class Person(models.Model):
  20
+class Author(models.Model):
12 21
     first_name = models.CharField(maxlength=30, db_column='firstname')
13 22
     last_name = models.CharField(maxlength=30, db_column='last')
14 23
 
15 24
     def __str__(self):
16 25
         return '%s %s' % (self.first_name, self.last_name)
17 26
 
  27
+    class Meta:
  28
+        db_table = 'my_author_table'
  29
+        ordering = ('last_name','first_name')
  30
+
  31
+class Article(models.Model):
  32
+    headline = models.CharField(maxlength=100)
  33
+    authors = models.ManyToManyField(Author, db_table='my_m2m_table')
  34
+
  35
+    def __str__(self):
  36
+        return self.headline
  37
+
  38
+    class Meta:
  39
+        ordering = ('headline',)
  40
+        
18 41
 __test__ = {'API_TESTS':"""
19  
-# Create a Person.
20  
->>> p = Person(first_name='John', last_name='Smith')
21  
->>> p.save()
  42
+# Create a Author.
  43
+>>> a = Author(first_name='John', last_name='Smith')
  44
+>>> a.save()
22 45
 
23  
->>> p.id
  46
+>>> a.id
24 47
 1
25 48
 
26  
->>> Person.objects.all()
27  
-[<Person: John Smith>]
  49
+# Create another author
  50
+>>> a2 = Author(first_name='Peter', last_name='Jones')
  51
+>>> a2.save()
  52
+
  53
+# Create an article
  54
+>>> art = Article(headline='Django lets you build web apps easily')
  55
+>>> art.save()
  56
+>>> art.authors = [a, a2]
28 57
 
29  
->>> Person.objects.filter(first_name__exact='John')
30  
-[<Person: John Smith>]
  58
+# Although the table and column names on Author have been set to 
  59
+# custom values, nothing about using the Author model has changed...
31 60
 
32  
->>> Person.objects.get(first_name__exact='John')
33  
-<Person: John Smith>
  61
+# Query the available authors
  62
+>>> Author.objects.all()
  63
+[<Author: Peter Jones>, <Author: John Smith>]
34 64
 
35  
->>> Person.objects.filter(firstname__exact='John')
  65
+>>> Author.objects.filter(first_name__exact='John')
  66
+[<Author: John Smith>]
  67
+
  68
+>>> Author.objects.get(first_name__exact='John')
  69
+<Author: John Smith>
  70
+
  71
+>>> Author.objects.filter(firstname__exact='John')
36 72
 Traceback (most recent call last):
37 73
     ...
38 74
 TypeError: Cannot resolve keyword 'firstname' into field
39 75
 
40  
->>> p = Person.objects.get(last_name__exact='Smith')
41  
->>> p.first_name
  76
+>>> a = Author.objects.get(last_name__exact='Smith')
  77
+>>> a.first_name
42 78
 'John'
43  
->>> p.last_name
  79
+>>> a.last_name
44 80
 'Smith'
45  
->>> p.firstname
  81
+>>> a.firstname
46 82
 Traceback (most recent call last):
47 83
     ...
48  
-AttributeError: 'Person' object has no attribute 'firstname'
49  
->>> p.last
  84
+AttributeError: 'Author' object has no attribute 'firstname'
  85
+>>> a.last
50 86
 Traceback (most recent call last):
51 87
     ...
52  
-AttributeError: 'Person' object has no attribute 'last'
  88
+AttributeError: 'Author' object has no attribute 'last'
  89
+
  90
+# Although the Article table uses a custom m2m table, 
  91
+# nothing about using the m2m relationship has changed...
  92
+
  93
+# Get all the authors for an article
  94
+>>> art.authors.all()
  95
+[<Author: Peter Jones>, <Author: John Smith>]
  96
+
  97
+# Get the articles for an author
  98
+>>> a.article_set.all()
  99
+[<Article: Django lets you build web apps easily>]
  100
+
  101
+# Query the authors across the m2m relation
  102
+>>> art.authors.filter(last_name='Jones')
  103
+[<Author: Peter Jones>]
  104
+
53 105
 """}

0 notes on commit 54feaca

Please sign in to comment.
Something went wrong with that request. Please try again.