Django-form_valid()与save() [英] Django - form_valid() vs save()

查看:47
本文介绍了Django-form_valid()与save()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django表单中,为了保存其他数据,我通常使用 form_valid(),但由于我也可以使用formclass的 save()方法.

In django forms, for saving other data I usually use form_valid() but as I can also use save() method of formclass.

今天,我覆盖了 save()而不是 form_valid(),我的manytomanyfield遇到了问题.

Today I overrided save() instead of form_valid() and I got problem with my manytomanyfield.

使用时,不会保存manytomanyfield的值,但是当我使用 form_valid()时,它们开始保存.谁能告诉我两者的原因和不同之处,哪一种是最方便的方法?在什么情况下?

When using , the values of manytomanyfield are not saved but when I use form_valid() they start saving. Can anybody tell me the reason and what are the differences between both, which is the most convenient method and in what situation?

这是我对save()方法的重载:

Here is my overriding of save() method:

class ProductCreateForm(forms.ModelForm):
    sizes = make_ajax_field(ProductCreateModel,'sizes','sizes')
    colours = make_ajax_field(ProductCreateModel,'colours','colours')

    class Meta:
        model = ProductCreateModel

        fields = ('title','category',
                    'regions',)

    def __init__(self,*args,**kwargs):
        self.request = kwargs.pop("request")
        super(ProductCreateForm, self).__init__(*args, **kwargs)

    def save(self):
        product = super(ProductCreateForm, self).save(commit=False)
        user =  self.request.user

        product.location = user.user_location
        product.save()
        return product

当我重写form_valid()方法时:

When I override form_valid() method:

   def get_form_kwargs(self):
       kwargs = super(ProductCreateView,self).get_form_kwargs()
       kwargs.update({'request':self.request})
       return kwargs

   def form_valid(self, form):
       product = form.save(commit=False)
       user =  self.request.user
       form.instance.user = user
       form.instance.location = user.user_location
       form.save()
       return super(ProductCreateView, self).form_valid(form)

sizes colours regions 是m2m字段,正如我在覆盖 save()值时提到的那样的m2m无法保存,但是当我覆盖 form_valid 时,它们开始保存.

sizes,colours and regions are m2m fields, as I mentioned when I overrides save() values of m2m not get saved but when I overrides form_valid they start saving.

推荐答案

如果使用 commit = False 保存表单,则必须调用表单的 save_m2m 方法进行保存多对多数据.有关更多信息,请参见文档.

If you save a form with commit=False, you must call the form's save_m2m method to save the many-to-many data. See the docs for more info.

如果您决定使用 form_valid 方法,我将更改以下内容:

If you decide to use the form_valid method, I would change the following things:

  • 更新由 form.save()返回的实例并保存它,而不是再次调用 form.save().
  • 明确调用 form.save_m2m()
  • 返回重定向响应,而不是调用 super().form_valid()(这将再次保存表单)
  • update the instance returned by form.save() and save it, instead of calling form.save() again.
  • explicitly call form.save_m2m()
  • return a redirect response instead of calling super().form_valid() (which will save the form again)

将它们放在一起,您将得到:

Putting that together, you get:

def form_valid(self, form):
    product = form.save(commit=False)
    product.user =  self.request.user
    product.location.location = user.user_location
    product.save()
    form.save_m2m()
    return redirect('/success-url/')

这篇关于Django-form_valid()与save()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆