Ticket #1143: regression_and_reverse_m2m.patch

File regression_and_reverse_m2m.patch, 6.4 KB (added by freakboy@…, 14 years ago)

Patch for regression of query behaviour, and reverse m2m queries

  • django/db/models/query.py

     
    236236                    # is set to True, which means the kwarg was bad.
    237237                    # Example: choices.get_list(poll__exact='foo')
    238238                    throw_bad_kwarg_error(kwarg)
    239                 # Try many-to-many relationships first...
     239                # Try many-to-many relationships in the direction in which they are
     240                # originally defined (i.e., the class that defines the ManyToManyField)
    240241                for f in current_opts.many_to_many:
    241242                    if f.name == current:
    242                         rel_table_alias = backend.quote_name(current_table_alias + LOOKUP_SEPARATOR + current)
     243                        rel_table_alias = backend.quote_name("m2m_" + current_table_alias + LOOKUP_SEPARATOR + current)
    243244
    244245                        joins[rel_table_alias] = (
    245246                            backend.quote_name(f.get_m2m_db_table(current_opts)),
     
    275276                            param_required = True
    276277                        current_opts = f.rel.to._meta
    277278                        raise StopIteration
     279                # Try many-to-many relationships first in the reverse direction
     280                # (i.e., from the class does not have the ManyToManyField)
     281                for f in current_opts.get_all_related_many_to_many_objects():
     282                    if f.name == current:
     283                        rel_table_alias = backend.quote_name("m2m_" + current_table_alias + LOOKUP_SEPARATOR + current)
     284
     285                        joins[rel_table_alias] = (
     286                            backend.quote_name(f.field.get_m2m_db_table(f.opts)),
     287                            "INNER JOIN",
     288                            '%s.%s = %s.%s' %
     289                                (backend.quote_name(current_table_alias),
     290                                backend.quote_name(current_opts.pk.column),
     291                                rel_table_alias,
     292                                backend.quote_name(current_opts.object_name.lower() + '_id'))
     293                        )
     294
     295                        # Optimization: In the case of primary-key lookups, we
     296                        # don't have to do an extra join.
     297                        if lookup_list and lookup_list[0] == f.opts.pk.name and lookup_type == 'exact':
     298                            where.append(get_where_clause(lookup_type, rel_table_alias+'.',
     299                                f.opts.object_name.lower()+'_id', kwarg_value))
     300                            params.extend(f.field.get_db_prep_lookup(lookup_type, kwarg_value))
     301                            lookup_list.pop()
     302                            param_required = False
     303                        else:
     304                            new_table_alias = current_table_alias + LOOKUP_SEPARATOR + current
     305
     306                            joins[backend.quote_name(new_table_alias)] = (
     307                                backend.quote_name(f.opts.db_table),
     308                                "INNER JOIN",
     309                                '%s.%s = %s.%s' %
     310                                    (rel_table_alias,
     311                                    backend.quote_name(f.opts.object_name.lower() + '_id'),
     312                                    backend.quote_name(new_table_alias),
     313                                    backend.quote_name(f.opts.pk.column))
     314                            )
     315                            current_table_alias = new_table_alias
     316                            param_required = True
     317                        current_opts = f.opts
     318                        raise StopIteration                                                                       
    278319                for f in current_opts.fields:
    279320                    # Try many-to-one relationships...
    280321                    if f.rel and f.name == current:
  • tests/modeltests/one_to_one/models.py

     
    6666
    6767>>> Restaurant.objects.get_object(place__id__exact=1)
    6868Demon Dogs the restaurant
     69>>> Restaurant.objects.get_object(place__name__startswith="Demon")
     70Demon Dogs the restaurant
    6971>>> Restaurant.objects.get_object(pk=1)
    7072Demon Dogs the restaurant
    7173
  • tests/modeltests/many_to_many/models.py

     
    2828>>> p1.save()
    2929>>> p2 = Publication(id=None, title='Science News')
    3030>>> p2.save()
     31>>> p3 = Publication(id=None, title='Science Weekly')
     32>>> p3.save()
    3133
    3234# Create an Article.
    3335>>> a1 = Article(id=None, headline='Django lets you build Web apps easily')
     
    5052True
    5153>>> a2.set_publications([p1.id])
    5254True
    53 >>> a2.set_publications([p1.id, p2.id])
     55>>> a2.set_publications([p1.id, p2.id, p3.id])
    5456True
    5557
    5658# Article objects have access to their related Publication objects.
    5759>>> a1.get_publication_list()
    5860[The Python Journal]
    5961>>> a2.get_publication_list()
    60 [The Python Journal, Science News]
     62[The Python Journal, Science News, Science Weekly]
    6163
    6264# Publication objects have access to their related Article objects.
    6365>>> p2.get_article_list()
     
    6567>>> p1.get_article_list(order_by=['headline'])
    6668[Django lets you build Web apps easily, NASA uses Python]
    6769
     70# We can perform kwarg queries across m2m relationships
     71>>> Article.objects.get_list(publications__pk=1)
     72[Django lets you build Web apps easily, NASA uses Python]
     73
     74>>> Article.objects.get_list(publications__title__startswith="Science")
     75[NASA uses Python, NASA uses Python]
     76
     77>>> Article.objects.get_list(publications__title__startswith="Science", distinct=True)
     78[NASA uses Python]
     79
     80# Reverse m2m queries (i.e., start at the table that doesn't have a ManyToManyField)
     81>>> Publication.objects.get_list(articles__headline__startswith="NASA")
     82[The Python Journal, Science News, Science Weekly]
     83
     84>>> Publication.objects.get_list(articles__pk=1)
     85[The Python Journal]
     86
    6887# If we delete a Publication, its Articles won't be able to access it.
    6988>>> p1.delete()
    7089>>> Publication.objects.get_list()
    71 [Science News]
     90[Science News, Science Weekly]
    7291>>> a1 = Article.objects.get_object(pk=1)
    7392>>> a1.get_publication_list()
    7493[]
Back to Top