Ticket #2243: django_2243b.patch

File django_2243b.patch, 5.4 KB (added by yk4ever@…, 18 years ago)

Updated version of patch, now everything works correctly

  • django/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
     
    671671        new_data = {}
    672672        if obj:
    673673            instance_ids = [instance._get_pk_val() for instance in getattr(obj, self.name).all()]
    674             if self.rel.raw_id_admin:
    675                 new_data[self.name] = ",".join([str(id) for id in instance_ids])
    676             else:
    677                 new_data[self.name] = instance_ids
     674            new_data[self.name] = instance_ids
    678675        else:
    679676            # In required many-to-many fields with only one available choice,
    680677            # select that one available choice.
  • django/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.
  • django/forms/__init__.py

     
    971971            raise validators.CriticalValidationError, e.messages
    972972
    973973class RawIdAdminField(CommaSeparatedIntegerField):
     974    # well, renderer should see data as LIST...
     975    requires_data_list = True
     976
     977    # ... whereas validators are to see it as STRING
     978    def run_validator(self, new_data, validator):
     979        if self.is_required or new_data.get(self.field_name, False) or hasattr(validator, 'always_test'):
     980            validator(new_data.get(self.field_name, ''), new_data)
     981
     982    # here we change actual data from STRING to LIST
     983    def convert_post_data(self, new_data):
     984        name = self.get_member_name()
     985        if new_data.has_key(self.field_name):
     986            d = new_data[self.field_name]
     987            converted_data = self.__class__.html2python(d)
     988            new_data.setlist(name, converted_data)
     989        else:
     990            new_data.setlist(name, [])
     991
    974992    def html2python(data):
    975993        return data.split(',')
    976994    html2python = staticmethod(html2python)
    977995
     996    def render(self, data):
     997        # it HAS to be list, but just in case...
     998        if isinstance(data, list):
     999            data = ','.join(str(d) for d in data)
     1000        return TextField.render(self, data)
     1001
    9781002class XMLLargeTextField(LargeTextField):
    9791003    """
    9801004    A LargeTextField with an XML validator. The schema_path argument is the
Back to Top