Skip to content

Latest commit

 

History

History
12 lines (9 loc) · 380 Bytes

20221205115023.md

File metadata and controls

12 lines (9 loc) · 380 Bytes

Django form save commit kwarg

If you want to add extra attributes when saving a ModelForm, you can save the form with commit=False. I commonly use this to set a foreign key field for example.

if form.is_valid()  # some Django ModelForm object
    bite = form.save(commit=False)
    bite.user = request.user  # set the user foreign key field
    bite.save()

#Django