Ticket #10992: 10992_fix.diff

File 10992_fix.diff, 1.5 KB (added by zain, 6 years ago)

Fix for this bug

  • django/forms/models.py

     
    604603        if not self.get_queryset():
    605604            return []
    606605
    607         # Put the objects from self.get_queryset into a dict so they are easy to lookup by pk
    608         existing_objects = {}
    609         for obj in self.get_queryset():
    610             existing_objects[obj.pk] = obj
    611606        saved_instances = []
    612607        for form in self.initial_forms:
    613608            pk_name = self._pk_field.name
    614609            raw_pk_value = form._raw_value(pk_name)
    615             pk_value = form.fields[pk_name].clean(raw_pk_value).pk
    616             obj = existing_objects[pk_value]
     610            clean_data = form.fields[pk_name].clean(raw_pk_value)
     611           
     612            if isinstance(form.fields[pk_name], ModelChoiceField):
     613                # the PK field is the default implicit PK field (ModelChoiceField), and
     614                # ModelChoiceField.clean() returns an object, not a raw primary key value
     615                clean_pk = clean_data.pk
     616            else:
     617                # the PK field is of another type, like CharField or FloatField
     618                clean_pk = clean_data
     619           
     620            obj = self.get_queryset().get(pk=clean_pk)
     621
    617622            if self.can_delete:
    618623                raw_delete_value = form._raw_value(DELETION_FIELD_NAME)
    619624                should_delete = form.fields[DELETION_FIELD_NAME].clean(raw_delete_value)
Back to Top