user7795844
user7795844

Reputation:

Django ModelForm not saving data even though form.save is executed

I have a website where user have 2 model for their profile, user_detail and user_location. I tried to serve 2 model form on one page with one submit. The problem is when the data from those model form does not save in to the database.

I confirmed that self.request.POST in the post method returns the correct data.

I tried :

Django ModelForm not saving data to database - Does not work

Django ModelForm not saving data - Does not work

The following code if for admins.

Here is my view :

class UpdateProfile(LoginRequiredMixin, UpdateView):
    template_name = 'account/user_profile.html'
    fields = '__all__'
    model = models.UserProfile
    user_detail_form_class = forms.UserDetailForm
    user_location_form_class = forms.UserLocationForm

    def get_context_data(self, **kwargs):
        user_profile = get_object_or_404(models.UserProfile, pk=self.kwargs.get(self.pk_url_kwarg))

        context = super(UpdateProfile, self).get_context_data(**kwargs)
        if 'user_detail_form' not in context:
            context['user_detail_form'] = self.user_detail_form_class(instance=user_profile.user_detail)
        if 'user_location_form' not in context:
            context['user_location_form'] = self.user_location_form_class(instance=user_profile.user_location)

        return context

    def get(self, request, *args, **kwargs):
        super(UpdateProfile, self).get(request, *args, **kwargs)
        return self.render_to_response(self.get_context_data())

    def post(self, request, *args, **kwargs):
        user_detail_form = self.user_detail_form_class(request.POST)
        user_location_form = self.user_location_form_class(request.POST)

        if user_detail_form.is_valid() and user_location_form.is_valid():
            user_detail_form.save()
            user_location_form.save()
            return redirect(self.get_success_url())
        else:
            return self.render_to_response(self.get_context_data())

        def get_success_url(self):
            return reverse('account:admin_client_list')

        def dispatch(self, request, *args, **kwargs):
            if not request.user.groups.filter(name__in=['Admin']).exists():
                 return errors.render_403(request)

            return super(UpdateProfile, self).dispatch(request, *args, **kwargs)

Here is my template :

{% extends 'base.html' %}

{% block content %}
<form method='POST' action="">{% csrf_token %}
    {{ user_detail_form }}
    {{ user_location_form }}

    <input type="submit" value="Submit">
</form>

{% endblock %}

Here is the form :

class UserDetailForm(forms.ModelForm):
    class Meta:
        model = models.UserDetail
        fields = '__all__'


class UserLocationForm(forms.ModelForm):
    class Meta:
        model = models.UserLocation
        fields = '__all__'

Upvotes: 0

Views: 622

Answers (1)

Nayan
Nayan

Reputation: 1605

You need to pass the instance parameter when you are creating the ModelForm in the post method. Sample code:

user_profile = get_object_or_404(models.UserProfile, pk=self.kwargs.get(self.pk_url_kwarg))
user_detail_form = self.user_detail_form_class(request.POST, instance=user_profile.user_detail)
user_location_form = self.user_location_form_class(request.POST, instance=user_profile.user_location)

Upvotes: 3

Related Questions