Ticket #10922: patch10922-r10844.diff
File patch10922-r10844.diff, 2.1 KB (added by , 15 years ago) |
---|
-
django/forms/models.py
464 464 return len(self.get_queryset()) 465 465 return super(BaseModelFormSet, self).initial_form_count() 466 466 467 def _construct_forms(self, *args, **kwargs): 468 if self.is_bound: 469 self._queryset_dict = dict([(o.pk, o) for o in self.get_queryset()]) 470 super(BaseModelFormSet, self)._construct_forms(*args, **kwargs) 471 if self.is_bound: 472 del self._queryset_dict 473 467 474 def _construct_form(self, i, **kwargs): 468 if i < self.initial_form_count(): 475 if self.is_bound and i < self.initial_form_count(): 476 pk_key = "%s-%s" % (self.add_prefix(i), self.model._meta.pk.name) 477 pk = self.data[pk_key] 478 pk_field = self.model._meta.pk 479 pk = pk_field.get_db_prep_lookup('exact', pk) 480 if isinstance(pk, list): 481 pk = pk[0] 482 kwargs['instance'] = self._queryset_dict.get(pk) 483 if i < self.initial_form_count() and not kwargs.get('instance'): 469 484 kwargs['instance'] = self.get_queryset()[i] 470 485 return super(BaseModelFormSet, self)._construct_form(i, **kwargs) 471 486 … … 663 678 return ((not pk.editable) or (pk.auto_created or isinstance(pk, AutoField)) 664 679 or (pk.rel and pk.rel.parent_link and pk_is_not_editable(pk.rel.to._meta.pk))) 665 680 if pk_is_not_editable(pk) or pk.name not in form.fields: 666 try: 667 pk_value = self.get_queryset()[index].pk 668 except IndexError: 669 pk_value = None 681 if form.is_bound: 682 pk_value = form.instance.pk 683 else: 684 try: 685 pk_value = self.get_queryset()[index].pk 686 except IndexError: 687 pk_value = None 670 688 if isinstance(pk, OneToOneField) or isinstance(pk, ForeignKey): 671 689 qs = pk.rel.to._default_manager.get_query_set() 672 690 else: