'Add a hidden field for the object\'s primary key.'

def add_fields(self, form, index):

    from django.db.models import AutoField, OneToOneField, ForeignKey
    self._pk_field = pk = self.model._meta.pk
    def pk_is_not_editable(pk):
        return ((not pk.editable) or (pk.auto_created or isinstance(pk, AutoField)) or (pk.rel and pk.rel.parent_link and pk_is_not_editable(pk.rel.to._meta.pk)))
    if (pk_is_not_editable(pk) or (pk.name not in form.fields)):
        if form.is_bound:
            pk_value = form.instance.pk
        else:
            try:
                if (index is not None):
                    pk_value = self.get_queryset()[index].pk
                else:
                    pk_value = None
            except IndexError:
                pk_value = None
        if (isinstance(pk, OneToOneField) or isinstance(pk, ForeignKey)):
            qs = pk.rel.to._default_manager.get_query_set()
        else:
            qs = self.model._default_manager.get_query_set()
        qs = qs.using(form.instance._state.db)
        form.fields[self._pk_field.name] = ModelChoiceField(qs, initial=pk_value, required=False, widget=HiddenInput)
    super(BaseModelFormSet, self).add_fields(form, index)
