Django-具有多种模型形式的UpdateView-一个提交

时间:2019-05-06 14:49:41

标签: django

我正在开发一个网站,用户可以在该网站上注册和编辑他们的信息。我想在单个页面中使用多种形式。我可以使用CreateView开发注册表单。但是,当我使用UpdateVIew时,我不知道如何以多种形式添加初始值。

我的代码(view.py)如下。我可以添加form数据作为初始值,但不能添加form2。请告诉我如何添加form2数据作为初始值。

■️view.py

class UserUpdate(OnlyYouMixin, generic.UpdateView):
    model = Profile
    form_class = ProfileForm
    second_form_class = ProfileNearStationForm
    template_name = 'accounts/user_form.html'

    def get_context_data(self, **kwargs):
        context = super(UserUpdate, self).get_context_data(**kwargs)

        if 'form' not in context:
            context['form'] = self.form_class(self.request.GET, instance=self.request.user)
        if 'form2' not in context:
            context['form2'] = self.second_form_class(self.request.GET, instance=self.request.user)
        return context

    def get(self, request, *args, **kwargs):
        super(UserUpdate, self).get(request, *args, **kwargs)
        form = self.form_class
        form2 = self.second_form_class
        return self.render_to_response(self.get_context_data())

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        form = self.form_class(request.POST, instance=self.request.user)
        form2 = self.second_form_class(request.POST, instance=self.request.user)

        if form.is_valid() and form2.is_valid():
            profile = form.save(commit=False)
            profile.save()
            station = form2.save(commit=False)
            station.user = user
            station.save()
            messages.success(self.request, 'Settings saved successfully')
            return HttpResponseRedirect(self.get_success_url())
        else:
            return self.render_to_response(
              self.get_context_data(form=form, form2=form2))

    def get_success_url(self):
        return resolve_url('accounts:user_form', pk=self.kwargs['pk'])

■️form.py

class ProfileForm(forms.ModelForm):
    name = forms.CharField(required=True)
    tel_number = forms.CharField(required=True)

    class Meta:
        model = Profile
        fields = ('name', 'tel_number')

class ProfileNearStationForm(forms.ModelForm):
    prefecture = forms.ModelChoiceField(queryset=areas_model.Prefecture.objects.all(), required=True)
    railway = forms.ModelChoiceField(queryset=areas_model.Railway.objects.none(), required=True)
    station = forms.ModelChoiceField(queryset=areas_model.Station.objects.none(), required=True)
    memo = forms.CharField(required=False)

    class Meta:
        model = ProfileNearStation
        fields = ('prefecture', 'railway', 'station', 'memo')

■️model.py

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    name = models.CharField(max_length=255)
    tel_number = models.CharField(max_length=13)

class ProfileNearStation(models.Model):
    prefecture = models.ForeignKey(areas_model.Prefecture, on_delete=models.CASCADE)
    railway = models.ForeignKey(areas_model.Railway, on_delete=models.CASCADE)
    station = models.ForeignKey(areas_model.Station, on_delete=models.CASCADE)
    memo = models.CharField(max_length=255, null=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

0 个答案:

没有答案