Ticket #2243: django_2243.patch

File django_2243.patch, 4.2 KB (added by yk4ever@…, 18 years ago)

Fixes 2243; modifies raw_id_admin fields to behave more like common ones; shoos some minor bug

  • db/models/fields/related.py

     
    22from django.db.models import signals, get_model
    33from django.db.models.fields import AutoField, Field, IntegerField, get_ul_class
    44from django.db.models.related import RelatedObject
    5 from django.utils.translation import gettext_lazy, string_concat
     5from django.utils.translation import gettext_lazy, string_concat, ngettext
    66from django.utils.functional import curry
    77from django.core import validators
    88from django import forms
  • db/models/manipulators.py

     
    115115        # Save many-to-many objects. Example: Set sites for a poll.
    116116        for f in self.opts.many_to_many:
    117117            if self.follow.get(f.name, None):
    118                 if not f.rel.edit_inline:
    119                     if f.rel.raw_id_admin:
    120                         new_vals = new_data.get(f.name, ())
    121                     else:
    122                         new_vals = new_data.getlist(f.name)
     118                if not f.rel.edit_inline:
     119                    # PATCH for 2243: M2M fields give list regardless of raw_id_admin
     120                    new_vals = new_data.getlist(f.name)
    123121                    # First, clear the existing values.
    124122                    rel_manager = getattr(new_object, f.name)
    125123                    rel_manager.clear()
     
    212210                                    if not f.primary_key and f != related.field and str(getattr(old_rel_obj, f.attname)) != str(getattr(new_rel_obj, f.attname)):
    213211                                        self.fields_changed.append('%s for %s "%s"' % (f.verbose_name, related.opts.verbose_name, new_rel_obj))
    214212
    215                         # Save many-to-many objects.
    216                         for f in related.opts.many_to_many:
    217                             if child_follow.get(f.name, None) and not f.rel.edit_inline:
    218                                 was_changed = getattr(new_rel_obj, 'set_%s' % f.name)(rel_new_data[f.attname])
    219                                 if self.change and was_changed:
    220                                     self.fields_changed.append('%s for %s "%s"' % (f.verbose_name, related.opts.verbose_name, new_rel_obj))
     213                        # Save many-to-many objects - PATCH FOR 2243
     214                        for f in related.opts.many_to_many:
     215                            if child_follow.get(f.name, None) and not f.rel.edit_inline:
     216                                new_vals = rel_new_data.get(f.name, ())
     217                                # First, clear the existing values.
     218                                rel_manager = getattr(new_rel_obj, f.name)
     219                                rel_manager.clear()
     220                                # Then, set the new values.
     221                                for n in new_vals:
     222                                    rel_manager.add(f.rel.to._default_manager.get(pk=n))
     223                                # TODO: Add to 'fields_changed'
    221224
    222225                    # If, in the change stage, all of the core fields were blank and
    223226                    # the primary key (ID) was provided, delete the item.
  • forms/__init__.py

     
    971971            raise validators.CriticalValidationError, e.messages
    972972
    973973class RawIdAdminField(CommaSeparatedIntegerField):
     974    # we need to overload this. Why? Because we're transforming a SINGLE field to a LIST!
     975    # and we don't wanna end up having LIST of LISTS
     976    def convert_post_data(self, new_data):
     977        name = self.get_member_name()
     978        if new_data.has_key(self.field_name):
     979            d = new_data[self.field_name]
     980            converted_data = self.__class__.html2python(d)
     981            new_data.setlist(name, converted_data)
     982        else:
     983            new_data.setlist(name, [])
     984
    974985    def html2python(data):
    975986        return data.split(',')
    976987    html2python = staticmethod(html2python)
Back to Top