Ticket #785: m2m_column_names.patch
File m2m_column_names.patch, 5.4 KB (added by , 18 years ago) |
---|
-
django/db/models/fields/related.py
634 634 raw_id_admin=kwargs.pop('raw_id_admin', False), 635 635 symmetrical=kwargs.pop('symmetrical', True)) 636 636 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) 637 639 if kwargs["rel"].raw_id_admin: 638 640 kwargs.setdefault("validator_list", []).append(self.isValidIDList) 639 641 Field.__init__(self, **kwargs) … … 663 665 664 666 def _get_m2m_column_name(self, related): 665 667 "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 666 670 # 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: 668 672 return 'from_' + related.model._meta.object_name.lower() + '_id' 669 673 else: 670 674 return related.model._meta.object_name.lower() + '_id' 671 675 672 676 def _get_m2m_reverse_name(self, related): 673 677 "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 674 680 # 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: 676 682 return 'to_' + related.parent_model._meta.object_name.lower() + '_id' 677 683 else: 678 684 return related.parent_model._meta.object_name.lower() + '_id' -
docs/model-api.txt
893 893 data. If this is not provided, Django will assume a default 894 894 name based upon the names of the two tables being joined. 895 895 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 896 906 ======================= ============================================================ 897 907 898 908 One-to-one relationships -
tests/modeltests/custom_columns/models.py
28 28 db_table = 'my_author_table' 29 29 ordering = ('last_name','first_name') 30 30 31 class 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 31 39 class Article(models.Model): 32 40 headline = models.CharField(maxlength=100) 33 41 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') 34 43 35 44 def __str__(self): 36 45 return self.headline … … 51 60 >>> a2.save() 52 61 53 62 # 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] 57 66 58 67 # Although the table and column names on Author have been set to 59 68 # custom values, nothing about using the Author model has changed... … … 91 100 # nothing about using the m2m relationship has changed... 92 101 93 102 # Get all the authors for an article 94 >>> art .authors.all()103 >>> art1.authors.all() 95 104 [<Author: Peter Jones>, <Author: John Smith>] 96 105 97 106 # Get the articles for an author … … 99 108 [<Article: Django lets you build web apps easily>] 100 109 101 110 # Query the authors across the m2m relation 102 >>> art .authors.filter(last_name='Jones')111 >>> art1.authors.filter(last_name='Jones') 103 112 [<Author: Peter Jones>] 104 113 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 105 141 """}