4

感谢 Insin 回答了与此相关的上一个问题

他的回答很有效,而且效果很好,但是,我对“cleaned_data”的提供感到困惑,或者更准确地说,如何使用它?

class RegistrationFormPreview(FormPreview):
    preview_template    = 'workshops/workshop_register_preview.html'
    form_template       = 'workshops/workshop_register_form.html'

    def done(self, request, cleaned_data):
        # Do something with the cleaned_data, then redirect
        # to a "success" page. 

        registration            = Registration(cleaned_data)
        registration.user       = request.user
        registration.save()
        # an attempt to work with cleaned_data throws the error: TypeError 
        # int() argument must be a string or a number, not 'dict'
        # obviously the fk are python objects(?) and not fk_id
        # but how to proceed here in an easy way?



        # the following works fine, however, it seems to be double handling the POST data
        # which had already been processed in the django.formtools.preview.post_post
        # method, and passed through to this 'done' method, which is designed to 
        # be overidden.
        '''
        form                    = self.form(request.POST)   # instansiate the form with POST data
        registration            = form.save(commit=False)   # save before adding the user
        registration.user       = request.user              # add the user
        registration.save()                                 # and save.
        '''

        return HttpResponseRedirect('/register/success')

为了快速参考,这里是 post_post 方法的内容:

def post_post(self, request):
    "Validates the POST data. If valid, calls done(). Else, redisplays form."
    f = self.form(request.POST, auto_id=AUTO_ID)
    if f.is_valid():
        if self.security_hash(request, f) != request.POST.get(self.unused_name('hash')):
            return self.failed_hash(request) # Security hash failed.
        return self.done(request, f.cleaned_data)
    else:
        return render_to_response(self.form_template,
            {'form': f, 'stage_field': self.unused_name('stage'), 'state': self.state},
            context_instance=RequestContext(request))
4

1 回答 1

10

我以前从未尝试过您在这里使用 ModelForm 执行的操作,但是您可以使用 ** 运算符将您的cleaned_data 字典扩展为您的 Registration 构造函数所需的关键字参数:

   registration = Registration (**cleaned_data)

模型类的构造函数采用关键字参数,Django 的模型元类将其转换为结果对象的实例级属性。** 运算符是一种调用约定,它告诉 Python 将您的字典扩展为这些关键字参数。

换句话说...

你目前在做的事情无异于这样:

registration = Registration ({'key':'value', ...})

这不是您想要的,因为构造函数需要关键字参数,而不是包含关键字参数的字典。

你想做的是这个

registration = Registration (key='value', ...)

这与此类似:

registration = Registration (**{'key':'value', ...})

同样,我从未尝试过它,但只要您不对表单做任何花哨的事情,例如向其添加注册构造函数不期望的新属性,它似乎就可以工作。在这种情况下,您可能必须在执行此操作之前修改 clean_data 字典中的项目。

不过,通过表单预览实用程序,您似乎失去了 ModelForms 中固有的一些功能。或许您应该将您的用例带到 Django 邮件列表中,看看是否有对该 API 的潜在增强功能可以使其更好地与 ModelForms 一起使用。

编辑

除了我上面描述的内容之外,您始终可以“手动”从您的 clean_data 字典中提取字段并将它们也传递到您的 Registration 构造函数中,但需要注意的是,您必须记住在添加新代码时更新此代码字段到您的模型。

registration = Registration (
    x=cleaned_data['x'],
    y=cleaned_data['y'],
    z=cleaned_data['z'],
    ...
)
于 2009-03-09T23:46:11.767 回答