Changeset 4429
- Timestamp:
- 01/25/07 07:47:55 (2 years ago)
- Files:
-
- django/trunk/django/db/models/fields/related.py (modified) (2 diffs)
- django/trunk/docs/model-api.txt (modified) (1 diff)
- django/trunk/tests/modeltests/custom_columns/models.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/db/models/fields/related.py
r4389 r4429 630 630 raw_id_admin=kwargs.pop('raw_id_admin', False), 631 631 symmetrical=kwargs.pop('symmetrical', True)) 632 self.db_table = kwargs.pop('db_table', None) 632 633 if kwargs["rel"].raw_id_admin: 633 634 kwargs.setdefault("validator_list", []).append(self.isValidIDList) … … 652 653 def _get_m2m_db_table(self, opts): 653 654 "Function that can be curried to provide the m2m table name for this relation" 654 return '%s_%s' % (opts.db_table, self.name) 655 if self.db_table: 656 return self.db_table 657 else: 658 return '%s_%s' % (opts.db_table, self.name) 655 659 656 660 def _get_m2m_column_name(self, related): django/trunk/docs/model-api.txt
r4392 r4429 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 ======================= ============================================================ django/trunk/tests/modeltests/custom_columns/models.py
r3661 r4429 1 1 """ 2 17. Custom column names2 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') … … 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.id46 >>> 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() 28 52 29 >>> Person.objects.filter(first_name__exact='John') 30 [<Person: John Smith>] 53 # Create an article 54 >>> art = Article(headline='Django lets you build web apps easily') 55 >>> art.save() 56 >>> art.authors = [a, a2] 31 57 32 >>> Person.objects.get(first_name__exact='John') 33 <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... 34 60 35 >>> Person.objects.filter(firstname__exact='John') 61 # Query the available authors 62 >>> Author.objects.all() 63 [<Author: Peter Jones>, <Author: John Smith>] 64 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_name76 >>> a = Author.objects.get(last_name__exact='Smith') 77 >>> a.first_name 42 78 'John' 43 >>> p.last_name79 >>> a.last_name 44 80 'Smith' 45 >>> p.firstname81 >>> a.firstname 46 82 Traceback (most recent call last): 47 83 ... 48 AttributeError: ' Person' object has no attribute 'firstname'49 >>> p.last84 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 """}
