[Answered ]-Django ModelForm not saving data that is added to request.POST or through form.save(commit=False)

2👍

See this example from django’s official documentation:

form = PartialAuthorForm(request.POST)
author = form.save(commit=False)
author.title = 'Mr'
author.save()

In your case (untested):

if request.method == 'POST':
    form = ModuleForm(request.POST)
    object = form.save(commit=False)
    object.prof = Profile.objects.get(user=request.user)
    object.save()

EDIT:
To clarify your comment on my answer:

moduleform.save(commit=False)
moduleform.prof = prof
moduleform.save()

Does not work because the form will never save prof on the instance because it is not part of the fields. This is why you HAVE TO set the prof on the model-level.

👤sphere

Leave a comment