Django

Code

Changeset 4429

Show
Ignore:
Timestamp:
01/25/07 07:47:55 (2 years ago)
Author:
russellm
Message:

Fixed #3098 -- Added db_table parameter to m2m fields, allowing the specification of a custom table name for the m2m table. Thanks, Wolfram Kriesing.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/fields/related.py

    r4389 r4429  
    630630            raw_id_admin=kwargs.pop('raw_id_admin', False), 
    631631            symmetrical=kwargs.pop('symmetrical', True)) 
     632        self.db_table = kwargs.pop('db_table', None) 
    632633        if kwargs["rel"].raw_id_admin: 
    633634            kwargs.setdefault("validator_list", []).append(self.isValidIDList) 
     
    652653    def _get_m2m_db_table(self, opts): 
    653654        "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) 
    655659 
    656660    def _get_m2m_column_name(self, related): 
  • django/trunk/docs/model-api.txt

    r4392 r4429  
    875875                             relationship, allowing ``ManyToMany`` relationships to be 
    876876                             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. 
    877881 
    878882    =======================  ============================================================ 
  • django/trunk/tests/modeltests/custom_columns/models.py

    r3661 r4429  
    11""" 
    2 17. Custom column names 
     217. Custom column/table names 
    33 
    44If your database column name is different than your model attribute, use the 
    55``db_column`` parameter. Note that you'll use the field's name, not its column 
    66name, in API usage. 
     7 
     8If 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  
     10query the database. 
     11 
     12If you need to use a table name for a many-to-many relationship that differs  
     13from the default generated name, use the ``db_table`` parameter on the  
     14ManyToMany field. This has no effect on the API for querying the database. 
     15 
    716""" 
    817 
    918from django.db import models 
    1019 
    11 class Person(models.Model): 
     20class Author(models.Model): 
    1221    first_name = models.CharField(maxlength=30, db_column='firstname') 
    1322    last_name = models.CharField(maxlength=30, db_column='last') 
     
    1625        return '%s %s' % (self.first_name, self.last_name) 
    1726 
     27    class Meta: 
     28        db_table = 'my_author_table' 
     29        ordering = ('last_name','first_name') 
     30 
     31class 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         
    1841__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() 
    2245 
    23 >>> p.id 
     46>>> a.id 
    24471 
    2548 
    26 >>> Person.objects.all() 
    27 [<Person: John Smith>] 
     49# Create another author 
     50>>> a2 = Author(first_name='Peter', last_name='Jones') 
     51>>> a2.save() 
    2852 
    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] 
    3157 
    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... 
    3460 
    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') 
    3672Traceback (most recent call last): 
    3773    ... 
    3874TypeError: Cannot resolve keyword 'firstname' into field 
    3975 
    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 
    4278'John' 
    43 >>> p.last_name 
     79>>> a.last_name 
    4480'Smith' 
    45 >>> p.firstname 
     81>>> a.firstname 
    4682Traceback (most recent call last): 
    4783    ... 
    48 AttributeError: 'Person' object has no attribute 'firstname' 
    49 >>> p.last 
     84AttributeError: 'Author' object has no attribute 'firstname' 
     85>>> a.last 
    5086Traceback (most recent call last): 
    5187    ... 
    52 AttributeError: 'Person' object has no attribute 'last' 
     88AttributeError: '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 
    53105"""}