Ticket #2579: fix_raw_id_admin_m2m.2.patch

File fix_raw_id_admin_m2m.2.patch, 1.2 KB (added by brendan.mcadams@…, 18 years ago)

[Version 2: Fixed a bug with original] Updated patch to correct ticket item. Allows for creating initially empty fields, and clearing fields.

  • django/db/models/manipulators.py

    old new  
    116116        for f in self.opts.many_to_many:
    117117            if self.follow.get(f.name, None):
    118118                if not f.rel.edit_inline:
     119
     120                    # First, clear the existing values.
     121                    rel_manager = getattr(new_object, f.name)
     122                    rel_manager.clear()
     123
    119124                    if f.rel.raw_id_admin:
    120125                        new_vals = new_data.get(f.name, ())
     126                        # Not handling 'empty' raw_id_admin fields properly
     127                        # If nothing was entered, skip the rel manager add
     128                        if len(new_vals) == 1 and '' == new_vals[0]:
     129                            continue
    121130                    else:
    122131                        new_vals = new_data.getlist(f.name)
    123                     # First, clear the existing values.
    124                     rel_manager = getattr(new_object, f.name)
    125                     rel_manager.clear()
    126132                    # Then, set the new values.
    127133                    for n in new_vals:
    128134                        rel_manager.add(f.rel.to._default_manager.get(pk=n))
Back to Top