Ticket #6707: django_m2m_set_method.diff

File django_m2m_set_method.diff, 819 bytes (added by favo <favo@…>, 8 years ago)

patch of set method

  • django/db/models/fields/related.py

     
    475475            raise AttributeError, "Manager must be accessed via instance"
    476476
    477477        manager = self.__get__(instance)
    478         manager.clear()
    479         manager.add(*value)
     478        # another implementation to set m2m, try to affect minimal rows.
     479        old_objects, target_objects = set(manager.all()), set(value)
     480        add_objects = target_objects - old_objects
     481        remove_objects =  old_objects - target_objects
     482        manager.remove(*remove_objects)
     483        manager.add(*add_objects)
    480484
    481485class ForeignKey(RelatedField, Field):
    482486    empty_strings_allowed = False
Back to Top