简体   繁体   English

保存inlineformset_factory数据

[英]saving inlineformset_factory data

@render_to('edit_operation.html')       
def edit_operation(request, pk):
    from forms import OpBaseForm, OperationForm
    from django.forms.models import inlineformset_factory

    op = Operation.objects.get(pk=pk)

    OpBaseFormSet = inlineformset_factory(Operation, OpBase, form=OpBaseForm, extra=5, )

    if request.method == "POST":
        form    = OperationForm(request.POST, instance=op)
        formset = OpBaseFormSet(request.POST, instance=op)

        if formset.is_valid() and form.is_valid():
            form.save()
            formset.save()   #error here

            return HttpResponseRedirect( op.get_absolute_url() )
    else:
        form    = OperationForm(instance=op)
        formset = OpBaseFormSet(instance=op)


    return {'operation': op, 'opform': form, 'formset': formset}

This seems pretty straightforward, but whenever I try to submit data (even unchanged data), I get this error: 这似乎很简单,但是每当我尝试提交数据(甚至是不变的数据)时,都会出现此错误:

Environment:

Request Method: POST
Request URL: http://localhost:8000/edit/operation/1/
Django Version: 1.0.2 final
Python Version: 2.6.2
Installed Applications:
['main',
 'django_extensions',
 'django.contrib.comments',
 'django.contrib.admindocs',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.gis',
 'django.contrib.humanize',
 'registration']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "/usr/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  86.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.6/dist-packages/annoying/decorators.py" in wrapper
  55.             output = function(request, *args, **kwargs)
File "/home/chris/Websites/jobmap/main/views.py" in edit_operation
  68.           formset.save()
File "/usr/lib/python2.6/dist-packages/django/forms/models.py" in save
  389.         return self.save_existing_objects(commit) + self.save_new_objects(commit)
File "/usr/lib/python2.6/dist-packages/django/forms/models.py" in save_existing_objects
  403.             obj = existing_objects[form.cleaned_data[self._pk_field.name]]

Exception Type: KeyError at /edit/operation/1/
Exception Value: None

Anyone have any idea what is causing this error? 任何人都知道导致此错误的原因是什么? If theres an error in the formset, then what is formset.is_valid() doing being 'True'? 如果formset.is_valid()存在错误,那么formset.is_valid()在做的是'True'是什么? Anyone at least have any ideas on how to debug this in order to find whats going wrong? 至少有人对如何调试它有什么想法,以便找出问题所在? Both forms work perfectly fine when used individually, and I have {{formset.management_form}} in my template. 两种形式单独使用时,它们都可以很好地工作,并且我的模板中有{{formset.management_form}}

Just a quick guess... is your OpBaseForm excluding the model pk? 只是一个快速的猜测...您的OpBaseForm是否不包括模型pk? Or, are you missing the pk field in your template? 或者,您是否缺少模板中的pk字段? In other words, if you are laying out the fields yourself you still need to include the pk field even though it's hidden. 换句话说,如果您自己对字段进行布局,那么即使隐藏了pk字段,您仍然需要包括pk字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM