From 895d23267115412ab7344ce7668adb1efa7f8e00 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Wed, 20 Jul 2005 21:56:12 +0000 Subject: [PATCH] Small code cleanup -- changed django.core.management.get_sql_delete to use get_m2m_db_table(), and moved get_m2m_db_table() from Field to ManyToManyField, because it's only used by the latter git-svn-id: http://code.djangoproject.com/svn/django/trunk@256 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management.py | 6 +++--- django/core/meta.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/django/core/management.py b/django/core/management.py index 093a184b3f..3904974627 100644 --- a/django/core/management.py +++ b/django/core/management.py @@ -91,7 +91,7 @@ def get_sql_create(mod): for klass in mod._MODELS: opts = klass._meta for f in opts.many_to_many: - table_output = ['CREATE TABLE %s_%s (' % (opts.db_table, f.name)] + table_output = ['CREATE TABLE %s (' % f.get_m2m_db_table(opts)] table_output.append(' id %s NOT NULL PRIMARY KEY,' % db.DATA_TYPES['AutoField']) table_output.append(' %s_id %s NOT NULL REFERENCES %s (%s),' % \ (opts.object_name.lower(), db.DATA_TYPES['IntegerField'], opts.db_table, opts.pk.name)) @@ -127,11 +127,11 @@ def get_sql_delete(mod): for f in opts.many_to_many: try: if cursor is not None: - cursor.execute("SELECT 1 FROM %s_%s LIMIT 1" % (opts.db_table, f.name)) + cursor.execute("SELECT 1 FROM %s LIMIT 1" % f.get_m2m_db_table(opts)) except: pass else: - output.append("DROP TABLE %s_%s;" % (opts.db_table, f.name)) + output.append("DROP TABLE %s;" % f.get_m2m_db_table(opts)) output.append("DELETE FROM packages WHERE label = '%s';" % mod._MODELS[0]._meta.app_label) output.append("DELETE FROM auth_permissions WHERE package = '%s';" % mod._MODELS[0]._meta.app_label) output.append("DELETE FROM content_types WHERE package = '%s';" % mod._MODELS[0]._meta.app_label) diff --git a/django/core/meta.py b/django/core/meta.py index cc21f8cc59..c6026e1f01 100644 --- a/django/core/meta.py +++ b/django/core/meta.py @@ -1614,10 +1614,6 @@ class Field(object): return [] raise TypeError, "Field has invalid lookup: %s" % lookup_type - def get_m2m_db_table(self, original_opts): - "Returns the name of the DB table for this field's relationship." - return '%s_%s' % (original_opts.db_table, self.name) - def has_default(self): "Returns a boolean of whether this field has a default value." return self.default != NOT_PROVIDED @@ -2067,6 +2063,10 @@ class ManyToManyField(Field): choices = self.get_choices(include_blank=False) return [curry(formfields.SelectMultipleField, size=min(max(len(choices), 5), 15), choices=choices)] + def get_m2m_db_table(self, original_opts): + "Returns the name of the many-to-many 'join' table." + return '%s_%s' % (original_opts.db_table, self.name) + class OneToOneField(IntegerField): def __init__(self, to, to_field=None, rel_name=None, **kwargs): kwargs['name'] = kwargs.get('name', 'id')