Model form save. Get the saved object

后端 未结 4 1541
遇见更好的自我
遇见更好的自我 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:13

    @Fabian, that's for editing an instance that wasn't previously included in your model form,

    E.g your model form covers name, age, gender but in your model there is another field maybe country and you want to add it by yourself not your user, so you reveal the required fields and then you use the above procedure to add the rest This worked for me on the above prob

    If form.is_valid()
        Current_form=form.save()
        My_new_id=Curret_form.id
    

    This printed the id of the newly created objected which I later saved in another model

提交回复
热议问题