To remove your form with values ββfrom an existing model instance, you need to use the instance argument in the model form:
def update_profile(request, username): profile = request.user.get_profile() edit_profile_form = EditProfileForm(request.POST or None, current_user=request.user, instance=profile) if request.method == 'POST': if edit_profile_form.is_valid(): editprofile.save() context = {'edit_profile_form': edit_profile_form} return render(request, 'pandaboard/editprofile.html', context)
To enter the current request.user, you can override __init__ of EditProfileForm by passing an additional keyword argument (or arg, it doesn't really matter), and slip out of kwargs before calling super, so you don't pass ModelForm a keyword argument that it doesn't expects:
class EditProfileForm(ModelForm): class Meta: model = Profile def __init__(self, *args, **kwargs): current_user = kwargs.pop('current_user') super(EditProfileForm, self).__init__(*args, **kwargs) self.fields['employer'] = current_user
Now you do not need to pass commit=False and manually set the value of employer in the view.
Brandon
source share