Ticket #3389: m2m_ids.diff
File m2m_ids.diff, 3.5 KB (added by , 18 years ago) |
---|
-
django/db/models/fields/related.py
316 316 # join_table: name of the m2m link table 317 317 # source_col_name: the PK colname in join_table for the source object 318 318 # target_col_name: the PK colname in join_table for the target object 319 # *objs - objects to add 319 # *objs - objects to add. Either object instances, or primary keys of object instances. 320 320 from django.db import connection 321 321 322 322 # If there aren't any objects, there is nothing to do. 323 323 if objs: 324 324 # Check that all the objects are of the right type 325 new_ids = set() 325 326 for obj in objs: 326 if not isinstance(obj, self.model): 327 raise ValueError, "objects to add() must be %s instances" % self.model._meta.object_name 327 if isinstance(obj, self.model): 328 new_ids.add(obj._get_pk_val()) 329 else: 330 new_ids.add(obj) 328 331 # Add the newly created or already existing objects to the join table. 329 332 # First find out which items are already added, to avoid adding them twice 330 new_ids = set([obj._get_pk_val() for obj in objs])331 333 cursor = connection.cursor() 332 334 cursor.execute("SELECT %s FROM %s WHERE %s = %%s AND %s IN (%s)" % \ 333 335 (target_col_name, self.join_table, source_col_name, … … 354 356 # If there aren't any objects, there is nothing to do. 355 357 if objs: 356 358 # Check that all the objects are of the right type 359 old_ids = set() 357 360 for obj in objs: 358 if not isinstance(obj, self.model): 359 raise ValueError, "objects to remove() must be %s instances" % self.model._meta.object_name 361 if isinstance(obj, self.model): 362 old_ids.add(obj._get_pk_val()) 363 else: 364 old_ids.add(obj) 360 365 # Remove the specified objects from the join table 361 old_ids = set([obj._get_pk_val() for obj in objs])362 366 cursor = connection.cursor() 363 367 cursor.execute("DELETE FROM %s WHERE %s = %%s AND %s IN (%s)" % \ 364 368 (self.join_table, source_col_name, -
tests/modeltests/many_to_many/models.py
203 203 >>> p2.article_set.all() 204 204 [<Article: Oxygen-free diet works wonders>] 205 205 206 # Recreate the article and Publication we just deleted. 206 # Relation sets can also be set using primary key values 207 >>> p2.article_set = [a4.id, a5.id] 208 >>> p2.article_set.all() 209 [<Article: NASA finds intelligent life on Earth>, <Article: Oxygen-free diet works wonders>] 210 >>> a4.publications.all() 211 [<Publication: Science News>] 212 >>> a4.publications = [p3.id] 213 >>> p2.article_set.all() 214 [<Article: Oxygen-free diet works wonders>] 215 >>> a4.publications.all() 216 [<Publication: Science Weekly>] 217 218 # Recreate the article and Publication we have deleted. 207 219 >>> p1 = Publication(id=None, title='The Python Journal') 208 220 >>> p1.save() 209 221 >>> a2 = Article(id=None, headline='NASA uses Python')