即使执行了form.save,Django ModelForm也不保存数据

时间:2018-03-29 04:18:12

标签: django django-models django-forms django-templates django-views

我有一个网站,用户的个人资料有2个模型,user_detail和user_location。我尝试在一个页面上提供2个模型表单,一个提交。问题是来自那些模型表单的数据没有保存到数据库中。

我确认post方法中的self.request.POST返回正确的数据。

我试过了:

Django ModelForm not saving data to database - 不起作用

Django ModelForm not saving data - 不起作用

以下代码,如果是管理员。

以下是我的观点:

array:27 [▼
  0 => "id"
  1 => "title"
  2 => "slug"
  3 => "imageOne"
  4 => "imageTwo"
  5 => "short_description"
  6 => "description"
  7 => "price"
  8 => "meta_description"
  9 => "meta_tags"
  10 => "arrivalDays"
  11 => "height"
  12 => "weight"
  13 => "lenght"
  14 => "width"
  15 => "sku"
  16 => "stock"
  17 => "label"
  18 => "label_from"
  19 => "label_to"
  20 => "label_color"
  21 => "status_id"
  22 => "brand_id"
  23 => "category_id"
  24 => "subcategory_id"
  25 => "created_at"
  26 => "updated_at"
]

这是我的模板:

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)

以下是表格:

{% extends 'base.html' %}

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

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

{% endblock %}

1 个答案:

答案 0 :(得分:2)

在post方法中创建instance时,需要传递ModelForm参数。示例代码:

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)