Ticket #1143: regression_and_reverse_m2m.patch
File regression_and_reverse_m2m.patch, 6.4 KB (added by , 19 years ago) |
---|
-
django/db/models/query.py
236 236 # is set to True, which means the kwarg was bad. 237 237 # Example: choices.get_list(poll__exact='foo') 238 238 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) 240 241 for f in current_opts.many_to_many: 241 242 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) 243 244 244 245 joins[rel_table_alias] = ( 245 246 backend.quote_name(f.get_m2m_db_table(current_opts)), … … 275 276 param_required = True 276 277 current_opts = f.rel.to._meta 277 278 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 278 319 for f in current_opts.fields: 279 320 # Try many-to-one relationships... 280 321 if f.rel and f.name == current: -
tests/modeltests/one_to_one/models.py
66 66 67 67 >>> Restaurant.objects.get_object(place__id__exact=1) 68 68 Demon Dogs the restaurant 69 >>> Restaurant.objects.get_object(place__name__startswith="Demon") 70 Demon Dogs the restaurant 69 71 >>> Restaurant.objects.get_object(pk=1) 70 72 Demon Dogs the restaurant 71 73 -
tests/modeltests/many_to_many/models.py
28 28 >>> p1.save() 29 29 >>> p2 = Publication(id=None, title='Science News') 30 30 >>> p2.save() 31 >>> p3 = Publication(id=None, title='Science Weekly') 32 >>> p3.save() 31 33 32 34 # Create an Article. 33 35 >>> a1 = Article(id=None, headline='Django lets you build Web apps easily') … … 50 52 True 51 53 >>> a2.set_publications([p1.id]) 52 54 True 53 >>> a2.set_publications([p1.id, p2.id ])55 >>> a2.set_publications([p1.id, p2.id, p3.id]) 54 56 True 55 57 56 58 # Article objects have access to their related Publication objects. 57 59 >>> a1.get_publication_list() 58 60 [The Python Journal] 59 61 >>> a2.get_publication_list() 60 [The Python Journal, Science News ]62 [The Python Journal, Science News, Science Weekly] 61 63 62 64 # Publication objects have access to their related Article objects. 63 65 >>> p2.get_article_list() … … 65 67 >>> p1.get_article_list(order_by=['headline']) 66 68 [Django lets you build Web apps easily, NASA uses Python] 67 69 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 68 87 # If we delete a Publication, its Articles won't be able to access it. 69 88 >>> p1.delete() 70 89 >>> Publication.objects.get_list() 71 [Science News ]90 [Science News, Science Weekly] 72 91 >>> a1 = Article.objects.get_object(pk=1) 73 92 >>> a1.get_publication_list() 74 93 []