Ticket #5390: related.field_name.diff
File related.field_name.diff, 4.3 KB (added by , 17 years ago) |
---|
-
db/models/fields/related.py
266 266 """Creates a manager that subclasses 'superclass' (which is a Manager) 267 267 and adds behavior for many-to-many related objects.""" 268 268 class ManyRelatedManager(superclass): 269 def __init__(self, model=None, core_filters=None, instance=None, symmetrical=None, 270 join_table=None, source_col_name=None, target_col_name=None): 269 def __init__(self, model=None, core_filters=None, instance=None, 270 symmetrical=None, join_table=None, source_col_name=None, 271 target_col_name=None, field_name=None): 271 272 super(ManyRelatedManager, self).__init__() 272 273 self.core_filters = core_filters 273 274 self.model = model … … 277 278 self.source_col_name = source_col_name 278 279 self.target_col_name = target_col_name 279 280 self._pk_val = self.instance._get_pk_val() 281 self.field_name = field_name 280 282 if self._pk_val is None: 281 283 raise ValueError("%r instance needs to have a primary key value before a many-to-many relationship can be used." % model) 282 284 … … 339 341 existing_ids = set([row[0] for row in cursor.fetchall()]) 340 342 341 343 # Add the ones that aren't there already 342 for obj_id in (new_ids - existing_ids): 344 new_ids = new_ids - existing_ids 345 for obj_id in new_ids: 343 346 cursor.execute("INSERT INTO %s (%s, %s) VALUES (%%s, %%s)" % \ 344 347 (self.join_table, source_col_name, target_col_name), 345 348 [self._pk_val, obj_id]) 349 350 added_objs = [obj for obj in objs if (isinstance(obj, self.model) and obj._get_pk_val() in new_ids) or obj in new_ids] 351 dispatcher.send(signal=signals.m2m_add_items, sender=self.model, 352 instance=self.instance, field_name=self.field_name, 353 objs=added_objs) 354 346 355 transaction.commit_unless_managed() 347 356 348 357 def _remove_items(self, source_col_name, target_col_name, *objs): … … 365 374 (self.join_table, source_col_name, 366 375 target_col_name, ",".join(['%s'] * len(old_ids))), 367 376 [self._pk_val] + list(old_ids)) 377 dispatcher.send(signal=signals.m2m_remove_items, sender=self.model, 378 instance=self.instance, field_name=self.field_name, 379 objs=objs) 368 380 transaction.commit_unless_managed() 369 381 370 382 def _clear_items(self, source_col_name): 371 383 # source_col_name: the PK colname in join_table for the source object 384 dispatcher.send(signal=signals.m2m_clear_items, sender=self.model, 385 instance=self.instance, field_name=self.field_name) 372 386 cursor = connection.cursor() 373 387 cursor.execute("DELETE FROM %s WHERE %s = %%s" % \ 374 388 (self.join_table, source_col_name), … … 405 419 symmetrical=False, 406 420 join_table=qn(self.related.field.m2m_db_table()), 407 421 source_col_name=qn(self.related.field.m2m_reverse_name()), 408 target_col_name=qn(self.related.field.m2m_column_name()) 422 target_col_name=qn(self.related.field.m2m_column_name()), 423 field_name=self.related.field.name 409 424 ) 410 425 411 426 return manager … … 446 461 symmetrical=(self.field.rel.symmetrical and instance.__class__ == rel_model), 447 462 join_table=qn(self.field.m2m_db_table()), 448 463 source_col_name=qn(self.field.m2m_column_name()), 449 target_col_name=qn(self.field.m2m_reverse_name()) 464 target_col_name=qn(self.field.m2m_reverse_name()), 465 field_name=self.field.name 450 466 ) 451 467 452 468 return manager -
db/models/signals.py
10 10 post_delete = object() 11 11 12 12 post_syncdb = object() 13 14 m2m_add_items = object() 15 m2m_remove_items = object() 16 m2m_clear_items = object()