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
This commit is contained in:
Adrian Holovaty 2005-07-20 21:56:12 +00:00
parent d9656947db
commit 895d232671
2 changed files with 7 additions and 7 deletions

View File

@ -91,7 +91,7 @@ def get_sql_create(mod):
for klass in mod._MODELS: for klass in mod._MODELS:
opts = klass._meta opts = klass._meta
for f in opts.many_to_many: 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(' id %s NOT NULL PRIMARY KEY,' % db.DATA_TYPES['AutoField'])
table_output.append(' %s_id %s NOT NULL REFERENCES %s (%s),' % \ 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)) (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: for f in opts.many_to_many:
try: try:
if cursor is not None: 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: except:
pass pass
else: 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 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 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) output.append("DELETE FROM content_types WHERE package = '%s';" % mod._MODELS[0]._meta.app_label)

View File

@ -1614,10 +1614,6 @@ class Field(object):
return [] return []
raise TypeError, "Field has invalid lookup: %s" % lookup_type 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): def has_default(self):
"Returns a boolean of whether this field has a default value." "Returns a boolean of whether this field has a default value."
return self.default != NOT_PROVIDED return self.default != NOT_PROVIDED
@ -2067,6 +2063,10 @@ class ManyToManyField(Field):
choices = self.get_choices(include_blank=False) choices = self.get_choices(include_blank=False)
return [curry(formfields.SelectMultipleField, size=min(max(len(choices), 5), 15), choices=choices)] 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): class OneToOneField(IntegerField):
def __init__(self, to, to_field=None, rel_name=None, **kwargs): def __init__(self, to, to_field=None, rel_name=None, **kwargs):
kwargs['name'] = kwargs.get('name', 'id') kwargs['name'] = kwargs.get('name', 'id')