UpdateView在django内联formset中给出了空白表单

时间:2016-10-22 03:50:24

标签: django inline inline-formset

我的CreateView运行正常,但是当我尝试更新此表单数据时,它会显示空白表单。没有显示数据。这是我的views.py:

@require_authenticated_permission(
'member.add_person')
class PersonCreate( FormsetMixin, CreateView):
    template_name = 'member/person_form.html'
    model = Person    
    form_class = MemberForm
    formset_class = PersonFormSet

@require_authenticated_permission(
'member.change_person')
class PersonUpdate( FormsetMixin,  UpdateView):
    template_name = 'member/person_form.html'
    model = Person    
    form_class = MemberForm
    formset_class = PersonFormSet

这是我的表格:

class MemberForm(ModelForm):

    class Meta:
        model = Person
        exclude = ('user',)       

    def __init__(self, *args, **kwargs):
        super(MemberForm, self).__init__(*args, **kwargs)

        self.fields['name'].widget.attrs['placeholder'] = 'Your full name'
        self.fields['tele_land'].label = 'Land phone'
        self.fields['tele_cell'].label = 'Cell phone'
        self.fields['passing_year'].label = 'Passing year'
        self.fields['passing_year'].help_text = 'According to your session year'


    def save(self, request, commit=True):
        person = super().save(commit=False)
        if not person.pk:
            person.user = get_user(request)
        if commit:
            person.save()
            self.save_m2m()
        return person

class ChildrenForm(ModelForm):
       class Meta:
        model = Children
        fields = '__all__'

PersonFormSet = inlineformset_factory(Person, Children, extra=0, min_num=1, fields=('child_name', 'child_birth_date','blood_group' ))

地址:

url(r'^person/create/$', views.PersonCreate.as_view(), name='person-create'),

url(r'^person/(?P<slug>[\w\-]+)/update/$', views.PersonUpdate.as_view(), name='person-update'),

我的formset mixin:

class FormsetMixin(object):
    object = None

    def get(self, request, *args, **kwargs):
        if getattr(self, 'is_update_view', False):
            self.object = self.get_object()
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        formset_class = self.get_formset_class()
        formset = self.get_formset(formset_class)
        return self.render_to_response(self.get_context_data(form=form, formset=formset))

    def post(self, request, *args, **kwargs):
        if getattr(self, 'is_update_view', False):
            self.object = self.get_object()
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        formset_class = self.get_formset_class()
        formset = self.get_formset(formset_class)
        if form.is_valid() and formset.is_valid():
            return self.form_valid(form, formset)
        else:
            return self.form_invalid(form, formset)

    def get_formset_class(self):
        return self.formset_class

    def get_formset(self, formset_class):
        return formset_class(**self.get_formset_kwargs())

    def get_formset_kwargs(self):
        kwargs = {
            'instance': self.object
        }
        if self.request.method in ('POST', 'PUT'):
            kwargs.update({
                'data': self.request.POST,
                'files': self.request.FILES,
            })
        return kwargs

    def form_valid(self, form, formset):
        self.object = form.save(self.request)
        formset.instance = self.object
        formset.save()
        return redirect(self.object.get_absolute_url())

    def form_invalid(self, form, formset):
        return self.render_to_response(self.get_context_data(form=form, formset=formset))

为什么我的UpdateView会提供空白表单?

2 个答案:

答案 0 :(得分:0)

原因是getattr(self, 'is_update_view', False)始终为假,因此您未加载任何self.object

我使用django-extra-viewsModelFormSetView的类似情况。如果您已经了解基于Django类的视图,并且可以为您节省大量代码以便自己编写和维护,那么它很容易使用。

顺便说一下,您应该实施get_queryset并预取Children

答案 1 :(得分:0)

在UpdateView中使用:is_update_view = True。它就像

class PersonUpdate( FormsetMixin,  UpdateView):
template_name = 'member/person_form.html'
model = Person    
is_update_view = True
form_class = MemberForm
formset_class = PersonFormSet