Model form save. Get the saved object

后端 未结 4 1532
遇见更好的自我
遇见更好的自我 2020-12-29 19:52

If I have a model form and save it like:

f = FormModel(request.POST)
if f.is_valid():
    f.save()

How can I get back that object that has

4条回答
  •  离开以前
    2020-12-29 20:39

    When you save a modelform, it returns the saved instance of the model. So all you have to do is assign it to a variable:

    f = MyModelForm(request.POST)
    if f.is_valid():
        m = f.save()
    

    You do not need to mess around with commit=False or any of that stuff unless you are handling more complex data.

提交回复
热议问题