diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index ddb99c90a4..28491491ff 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -615,6 +615,7 @@ class ManyToManyField(RelatedField, Field): filter_interface=kwargs.pop('filter_interface', None), limit_choices_to=kwargs.pop('limit_choices_to', None), symmetrical=kwargs.pop('symmetrical', True)) + self.db_table = kwargs.pop('db_table', None) Field.__init__(self, **kwargs) msg = gettext_lazy('Hold down "Control", or "Command" on a Mac, to select more than one.') @@ -629,7 +630,10 @@ class ManyToManyField(RelatedField, Field): def _get_m2m_db_table(self, opts): "Function that can be curried to provide the m2m table name for this relation" - return '%s_%s' % (opts.db_table, self.name) + if self.db_table: + return self.db_table + else: + return '%s_%s' % (opts.db_table, self.name) def _get_m2m_column_name(self, related): "Function that can be curried to provide the source column name for the m2m table" diff --git a/django/db/models/query.py b/django/db/models/query.py index cbfd09e8d7..51fa334e63 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1,5 +1,6 @@ from django.db import backend, connection, transaction from django.db.models.fields import DateField, FieldDoesNotExist +from django.db.models.fields.generic import GenericRelation from django.db.models import signals from django.dispatch import dispatcher from django.utils.datastructures import SortedDict @@ -979,18 +980,26 @@ def delete_objects(seen_objs): pk_list = [pk for pk,instance in seen_objs[cls]] for related in cls._meta.get_all_related_many_to_many_objects(): - for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): - cursor.execute("DELETE FROM %s WHERE %s IN (%s)" % \ - (qn(related.field.m2m_db_table()), - qn(related.field.m2m_reverse_name()), - ','.join(['%s' for pk in pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]])), - pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]) + if not isinstance(related.field, GenericRelation): + for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): + cursor.execute("DELETE FROM %s WHERE %s IN (%s)" % \ + (qn(related.field.m2m_db_table()), + qn(related.field.m2m_reverse_name()), + ','.join(['%s' for pk in pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]])), + pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]) for f in cls._meta.many_to_many: + if isinstance(f, GenericRelation): + from django.contrib.contenttypes.models import ContentType + query_extra = 'AND %s=%%s' % f.rel.to._meta.get_field(f.content_type_field_name).column + args_extra = [ContentType.objects.get_for_model(cls).id] + else: + query_extra = '' + args_extra = [] for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): - cursor.execute("DELETE FROM %s WHERE %s IN (%s)" % \ + cursor.execute(("DELETE FROM %s WHERE %s IN (%s)" % \ (qn(f.m2m_db_table()), qn(f.m2m_column_name()), - ','.join(['%s' for pk in pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]])), - pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]) + ','.join(['%s' for pk in pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]]))) + query_extra, + pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE] + args_extra) for field in cls._meta.fields: if field.rel and field.null and field.rel.to in seen_objs: for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): diff --git a/docs/model-api.txt b/docs/model-api.txt index 33742220a3..8abd88f7ec 100644 --- a/docs/model-api.txt +++ b/docs/model-api.txt @@ -874,6 +874,10 @@ the relationship should work. All are optional: force Django to add the descriptor for the reverse relationship, allowing ``ManyToMany`` relationships to be non-symmetrical. + + ``db_table`` The name of the table to create for storing the many-to-many + data. If this is not provided, Django will assume a default + name based upon the names of the two tables being joined. ======================= ============================================================ diff --git a/tests/modeltests/custom_columns/models.py b/tests/modeltests/custom_columns/models.py index e88fa80da2..c09ca05557 100644 --- a/tests/modeltests/custom_columns/models.py +++ b/tests/modeltests/custom_columns/models.py @@ -1,53 +1,105 @@ """ -17. Custom column names +17. Custom column/table names If your database column name is different than your model attribute, use the ``db_column`` parameter. Note that you'll use the field's name, not its column name, in API usage. + +If your database table name is different than your model name, use the +``db_table`` Meta attribute. This has no effect on the API used to +query the database. + +If you need to use a table name for a many-to-many relationship that differs +from the default generated name, use the ``db_table`` parameter on the +ManyToMany field. This has no effect on the API for querying the database. + """ from django.db import models -class Person(models.Model): +class Author(models.Model): first_name = models.CharField(maxlength=30, db_column='firstname') last_name = models.CharField(maxlength=30, db_column='last') def __str__(self): return '%s %s' % (self.first_name, self.last_name) -__test__ = {'API_TESTS':""" -# Create a Person. ->>> p = Person(first_name='John', last_name='Smith') ->>> p.save() + class Meta: + db_table = 'my_author_table' + ordering = ('last_name','first_name') ->>> p.id +class Article(models.Model): + headline = models.CharField(maxlength=100) + authors = models.ManyToManyField(Author, db_table='my_m2m_table') + + def __str__(self): + return self.headline + + class Meta: + ordering = ('headline',) + +__test__ = {'API_TESTS':""" +# Create a Author. +>>> a = Author(first_name='John', last_name='Smith') +>>> a.save() + +>>> a.id 1 ->>> Person.objects.all() -[] +# Create another author +>>> a2 = Author(first_name='Peter', last_name='Jones') +>>> a2.save() ->>> Person.objects.filter(first_name__exact='John') -[] +# Create an article +>>> art = Article(headline='Django lets you build web apps easily') +>>> art.save() +>>> art.authors = [a, a2] ->>> Person.objects.get(first_name__exact='John') - +# Although the table and column names on Author have been set to +# custom values, nothing about using the Author model has changed... ->>> Person.objects.filter(firstname__exact='John') +# Query the available authors +>>> Author.objects.all() +[, ] + +>>> Author.objects.filter(first_name__exact='John') +[] + +>>> Author.objects.get(first_name__exact='John') + + +>>> Author.objects.filter(firstname__exact='John') Traceback (most recent call last): ... TypeError: Cannot resolve keyword 'firstname' into field ->>> p = Person.objects.get(last_name__exact='Smith') ->>> p.first_name +>>> a = Author.objects.get(last_name__exact='Smith') +>>> a.first_name 'John' ->>> p.last_name +>>> a.last_name 'Smith' ->>> p.firstname +>>> a.firstname Traceback (most recent call last): ... -AttributeError: 'Person' object has no attribute 'firstname' ->>> p.last +AttributeError: 'Author' object has no attribute 'firstname' +>>> a.last Traceback (most recent call last): ... -AttributeError: 'Person' object has no attribute 'last' +AttributeError: 'Author' object has no attribute 'last' + +# Although the Article table uses a custom m2m table, +# nothing about using the m2m relationship has changed... + +# Get all the authors for an article +>>> art.authors.all() +[, ] + +# Get the articles for an author +>>> a.article_set.all() +[] + +# Query the authors across the m2m relation +>>> art.authors.filter(last_name='Jones') +[] + """} diff --git a/tests/modeltests/generic_relations/models.py b/tests/modeltests/generic_relations/models.py index eb64d7ec3d..2bfb55e618 100644 --- a/tests/modeltests/generic_relations/models.py +++ b/tests/modeltests/generic_relations/models.py @@ -65,14 +65,14 @@ __test__ = {'API_TESTS':""" # Objects with declared GenericRelations can be tagged directly -- the API # mimics the many-to-many API. ->>> lion.tags.create(tag="yellow") - ->>> lion.tags.create(tag="hairy") - >>> bacon.tags.create(tag="fatty") >>> bacon.tags.create(tag="salty") +>>> lion.tags.create(tag="yellow") + +>>> lion.tags.create(tag="hairy") + >>> lion.tags.all() [, ] @@ -105,4 +105,30 @@ __test__ = {'API_TESTS':""" [] >>> TaggedItem.objects.filter(content_type__pk=ctype.id, object_id=quartz.id) [] + +# If you delete an object with an explicit Generic relation, the related +# objects are deleted when the source object is deleted. +# Original list of tags: +>>> [(t.tag, t.content_type, t.object_id) for t in TaggedItem.objects.all()] +[('clearish', , 1), ('fatty', , 2), ('hairy', , 1), ('salty', , 2), ('shiny', , 2), ('yellow', , 1)] + +>>> lion.delete() +>>> [(t.tag, t.content_type, t.object_id) for t in TaggedItem.objects.all()] +[('clearish', , 1), ('fatty', , 2), ('salty', , 2), ('shiny', , 2)] + +# If Generic Relation is not explicitly defined, any related objects +# remain after deletion of the source object. +>>> quartz.delete() +>>> [(t.tag, t.content_type, t.object_id) for t in TaggedItem.objects.all()] +[('clearish', , 1), ('fatty', , 2), ('salty', , 2), ('shiny', , 2)] + +# If you delete a tag, the objects using the tag are unaffected +# (other than losing a tag) +>>> tag = TaggedItem.objects.get(id=1) +>>> tag.delete() +>>> bacon.tags.all() +[] +>>> [(t.tag, t.content_type, t.object_id) for t in TaggedItem.objects.all()] +[('clearish', , 1), ('salty', , 2), ('shiny', , 2)] + """}