#785: many2many table and legacy databases.
-------------------------------+--------------------------------------------
Reporter: [EMAIL PROTECTED] | Owner: mtredinnick
Status: new | Component: Database wrapper
Version: SVN | Resolution:
Keywords: | Stage: Accepted
Has_patch: 1 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
-------------------------------+--------------------------------------------
Changes (by [EMAIL PROTECTED]):
* has_patch: 0 => 1
* version: => SVN
Comment:
Ok so here is my first patch for Django. I have tested this against a
legacy database I am trying to write app for. I have updated
'custom_columns' testcase to use custom columns for m2m relation. However
I was unable run the testcase (getting some error with readline probably
ipython 8.0 problem). I apologize for the half-backed patch. Can somebody
please verify the testcase for me?
I have used ''db_self_col'' and ''db_remote_col'' as column names.
{{{
Index: django/db/models/fields/related.py
===================================================================
--- django/db/models/fields/related.py (revision 5233)
+++ django/db/models/fields/related.py (working copy)
@@ -634,6 +634,8 @@
raw_id_admin=kwargs.pop('raw_id_admin', False),
symmetrical=kwargs.pop('symmetrical', True))
self.db_table = kwargs.pop('db_table', None)
+ self.db_self_col = kwargs.pop('db_self_col', None)
+ self.db_remote_col = kwargs.pop('db_remote_col', None)
if kwargs["rel"].raw_id_admin:
kwargs.setdefault("validator_list",
[]).append(self.isValidIDList)
Field.__init__(self, **kwargs)
@@ -664,7 +666,9 @@
def _get_m2m_column_name(self, related):
"Function that can be curried to provide the source column name
for the m2m table"
# If this is an m2m relation to self, avoid the inevitable name
clash
- if related.model == related.parent_model:
+ if self.db_self_col:
+ return self.db_self_col
+ elif related.model == related.parent_model:
return 'from_' + related.model._meta.object_name.lower() +
'_id'
else:
return related.model._meta.object_name.lower() + '_id'
@@ -672,7 +676,9 @@
def _get_m2m_reverse_name(self, related):
"Function that can be curried to provide the related column name
for the m2m table"
# If this is an m2m relation to self, avoid the inevitable name
clash
- if related.model == related.parent_model:
+ if self.db_remote_col:
+ return self.db_remote_col
+ elif related.model == related.parent_model:
return 'to_' + related.parent_model._meta.object_name.lower()
+ '_id'
else:
return related.parent_model._meta.object_name.lower() + '_id'
Index: tests/modeltests/custom_columns/models.py
===================================================================
--- tests/modeltests/custom_columns/models.py (revision 5233)
+++ tests/modeltests/custom_columns/models.py (working copy)
@@ -30,7 +30,8 @@
class Article(models.Model):
headline = models.CharField(maxlength=100)
- authors = models.ManyToManyField(Author, db_table='my_m2m_table')
+ authors = models.ManyToManyField(Author, db_table='my_m2m_table',
+ db_self_col='m_article_id', db_remote_col='m_author_id')
def __str__(self):
return self.headline
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/785#comment:7>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---