diff --git a/django/forms/models.py b/django/forms/models.py
index 41380f2..d25f3c1 100644
a
|
b
|
class BaseModelFormSet(BaseFormSet):
|
395 | 395 | return len(self.get_queryset()) |
396 | 396 | return super(BaseModelFormSet, self).initial_form_count() |
397 | 397 | |
| 398 | def _construct_forms(self, *args, **kwargs): |
| 399 | if self.is_bound: |
| 400 | self._queryset_dict = dict([(o.pk, o) for o in self.get_queryset()]) |
| 401 | super(BaseModelFormSet, self)._construct_forms(*args, **kwargs) |
| 402 | if self.is_bound: |
| 403 | del self._queryset_dict |
| 404 | |
398 | 405 | def _construct_form(self, i, **kwargs): |
399 | | if i < self.initial_form_count(): |
| 406 | if self.is_bound and i < self.initial_form_count(): |
| 407 | pk = self.data["%s-%s" % (self.add_prefix(i), self.model._meta.pk.name)] |
| 408 | pk_field = self.model._meta.pk |
| 409 | pk = pk_field.get_db_prep_lookup('exact', pk) |
| 410 | if isinstance(pk, list): |
| 411 | pk = pk[0] |
| 412 | kwargs['instance'] = self._queryset_dict.get(pk) |
| 413 | if i < self.initial_form_count() and not kwargs.get('instance'): |
400 | 414 | kwargs['instance'] = self.get_queryset()[i] |
401 | 415 | return super(BaseModelFormSet, self)._construct_form(i, **kwargs) |
402 | 416 | |
… |
… |
class BaseModelFormSet(BaseFormSet):
|
499 | 513 | return ((not pk.editable) or (pk.auto_created or isinstance(pk, AutoField)) |
500 | 514 | or (pk.rel and pk.rel.parent_link and pk_is_editable(pk.rel.to._meta.pk))) |
501 | 515 | if pk_is_editable(pk): |
502 | | try: |
503 | | pk_value = self.get_queryset()[index].pk |
504 | | except IndexError: |
| 516 | if form.is_bound: |
| 517 | pk_value = form.instance.pk |
| 518 | else: |
505 | 519 | pk_value = None |
506 | 520 | if isinstance(pk, OneToOneField) or isinstance(pk, ForeignKey): |
507 | 521 | qs = pk.rel.to._default_manager.get_query_set() |
diff --git a/tests/modeltests/model_formsets/models.py b/tests/modeltests/model_formsets/models.py
index f30b212..4c319f7 100644
a
|
b
|
class CustomPrimaryKey(models.Model):
|
58 | 58 | class Place(models.Model): |
59 | 59 | name = models.CharField(max_length=50) |
60 | 60 | city = models.CharField(max_length=50) |
61 | | |
| 61 | |
62 | 62 | def __unicode__(self): |
63 | 63 | return self.name |
64 | 64 | |
… |
… |
class OwnerProfile(models.Model):
|
85 | 85 | |
86 | 86 | class Restaurant(Place): |
87 | 87 | serves_pizza = models.BooleanField() |
88 | | |
| 88 | |
89 | 89 | def __unicode__(self): |
90 | 90 | return self.name |
91 | 91 | |
… |
… |
True
|
573 | 573 | ... print book.title |
574 | 574 | Les Fleurs du Mal |
575 | 575 | |
576 | | Test inline formsets where the inline-edited object uses multi-table inheritance, thus |
| 576 | Test inline formsets where the inline-edited object uses multi-table inheritance, thus |
577 | 577 | has a non AutoField yet auto-created primary key. |
578 | 578 | |
579 | 579 | >>> AuthorBooksFormSet3 = inlineformset_factory(Author, AlternateBook, can_delete=False, extra=1) |
… |
… |
True
|
734 | 734 | ... 'ownerprofile-0-owner': u'1', |
735 | 735 | ... 'ownerprofile-0-age': u'55', |
736 | 736 | ... } |
737 | | >>> formset = FormSet(data, instance=owner) |
| 737 | >>> formset = FormSet(data, instance=owner) # this seems to be testing the behavior we want to avoid... hrm. |
738 | 738 | >>> formset.is_valid() |
739 | 739 | True |
740 | 740 | >>> formset.save() |
741 | 741 | [<OwnerProfile: Joe Perry is 55>] |
742 | 742 | |
743 | | # ForeignKey with unique=True should enforce max_num=1 |
| 743 | # ForeignKey with unique=True should enforce max_num=1 |
744 | 744 | |
745 | 745 | >>> FormSet = inlineformset_factory(Place, Location, can_delete=False) |
746 | 746 | >>> formset = FormSet(instance=place) |