Code

Ticket #785: m2m_column_names.patch

File m2m_column_names.patch, 5.4 KB (added by surajbarkale@…, 7 years ago)
  • django/db/models/fields/related.py

     
    634634            raw_id_admin=kwargs.pop('raw_id_admin', False), 
    635635            symmetrical=kwargs.pop('symmetrical', True)) 
    636636        self.db_table = kwargs.pop('db_table', None) 
     637        self.db_self_column = kwargs.pop('db_self_column', None) 
     638        self.db_related_column = kwargs.pop('db_related_column', None) 
    637639        if kwargs["rel"].raw_id_admin: 
    638640            kwargs.setdefault("validator_list", []).append(self.isValidIDList) 
    639641        Field.__init__(self, **kwargs) 
     
    663665 
    664666    def _get_m2m_column_name(self, related): 
    665667        "Function that can be curried to provide the source column name for the m2m table" 
     668        if self.db_self_column: 
     669            return self.db_self_column 
    666670        # If this is an m2m relation to self, avoid the inevitable name clash 
    667         if related.model == related.parent_model: 
     671        elif related.model == related.parent_model: 
    668672            return 'from_' + related.model._meta.object_name.lower() + '_id' 
    669673        else: 
    670674            return related.model._meta.object_name.lower() + '_id' 
    671675 
    672676    def _get_m2m_reverse_name(self, related): 
    673677        "Function that can be curried to provide the related column name for the m2m table" 
     678        if self.db_related_column: 
     679            return self.db_related_column 
    674680        # If this is an m2m relation to self, avoid the inevitable name clash 
    675         if related.model == related.parent_model: 
     681        elif related.model == related.parent_model: 
    676682            return 'to_' + related.parent_model._meta.object_name.lower() + '_id' 
    677683        else: 
    678684            return related.parent_model._meta.object_name.lower() + '_id' 
  • docs/model-api.txt

     
    893893                             data. If this is not provided, Django will assume a default 
    894894                             name based upon the names of the two tables being joined. 
    895895 
     896    ``db_self_column''       The name of the column for storing primary key of this model 
     897                             in the table created for storing the many-to-many data. If 
     898                             this is not provided, Django will assume a default name 
     899                             based upon the name of the current model. 
     900 
     901    ``db_related_column``    The name of the column for storing primary key of the related 
     902                             model in the table created for storing the many-to-many data. 
     903                             If this is not provided, Django will assume a default name 
     904                             based upon the name of the related model. 
     905 
    896906    =======================  ============================================================ 
    897907 
    898908One-to-one relationships 
  • tests/modeltests/custom_columns/models.py

     
    2828        db_table = 'my_author_table' 
    2929        ordering = ('last_name','first_name') 
    3030 
     31class Category(models.Model): 
     32    name = models.CharField(maxlength=20) 
     33    class Meta: 
     34       ordering = ('name',) 
     35 
     36    def __str__(self): 
     37        return self.name 
     38 
    3139class Article(models.Model): 
    3240    headline = models.CharField(maxlength=100) 
    3341    authors = models.ManyToManyField(Author, db_table='my_m2m_table') 
     42    categories = models.ManyToManyField(Category, db_self_column='m2m_article_id', db_related_column='m2m_category_id') 
    3443 
    3544    def __str__(self): 
    3645        return self.headline 
     
    5160>>> a2.save() 
    5261 
    5362# Create an article 
    54 >>> art = Article(headline='Django lets you build web apps easily') 
    55 >>> art.save() 
    56 >>> art.authors = [a, a2] 
     63>>> art1 = Article(headline='Django lets you build web apps easily') 
     64>>> art1.save() 
     65>>> art1.authors = [a, a2] 
    5766 
    5867# Although the table and column names on Author have been set to  
    5968# custom values, nothing about using the Author model has changed... 
     
    91100# nothing about using the m2m relationship has changed... 
    92101 
    93102# Get all the authors for an article 
    94 >>> art.authors.all() 
     103>>> art1.authors.all() 
    95104[<Author: Peter Jones>, <Author: John Smith>] 
    96105 
    97106# Get the articles for an author 
     
    99108[<Article: Django lets you build web apps easily>] 
    100109 
    101110# Query the authors across the m2m relation 
    102 >>> art.authors.filter(last_name='Jones') 
     111>>> art1.authors.filter(last_name='Jones') 
    103112[<Author: Peter Jones>] 
    104113 
     114# Create some categories 
     115>>> c1 = Category(name='Python') 
     116>>> c1.save() 
     117>>> c2 = Category(name='Django') 
     118>>> c2.save() 
     119 
     120# Assign categories to models 
     121>>> art1.categories = [c1, c2] 
     122>>> art2 = Article(headline='Dive into Python') 
     123>>> art2.save() 
     124>>> art2.categories = [c1] 
     125 
     126# Verify that m2m operations are performed with custom column names on m2m 
     127# relation table 
     128 
     129>>> c1.article_set.all() 
     130[<Article: Dive into Python>, <Article: Django lets you build web apps easily>] 
     131 
     132>>> c2.article_set.all() 
     133[<Article: Django lets you build web apps easily>] 
     134 
     135>>> art1.categories.all() 
     136[<Category: Django>, <Category: Python>] 
     137 
     138>>> art2.categories.all() 
     139[<Category: Python>] 
     140 
    105141"""}