Django:inline_formset的更新表单不起作用

时间:2019-01-21 11:58:32

标签: django django-forms django-views

这些是我的表格:

class MultijournaltotalForm(forms.ModelForm):

    class Meta:
        model = Multijournaltotal
        fields = ('Date', 'Total_Debit', 'Total_Credit')
        widgets = {
            'Date': DateInput(),
        }

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('User', None)        
        self.company = kwargs.pop('Company', None)
        super(MultijournaltotalForm, self).__init__(*args, **kwargs)
        self.fields['Date'].widget.attrs = {'class': 'form-control',}
        self.fields['Total_Debit'].widget.attrs = {'class': 'form-control',}
        self.fields['Total_Credit'].widget.attrs = {'class': 'form-control',}


class MultijournalForm(forms.ModelForm):

    class Meta:
        model = Multijournal
        fields = ('By','To','Debit','Credit','narration')


    def __init__(self, *args, **kwargs):
        self.User = kwargs.pop('User', None)        
        self.Company = kwargs.pop('Company', None)
        super(MultijournalForm, self).__init__(*args, **kwargs)
        self.fields['Debit'].widget.attrs = {'class': 'form-control',}
        self.fields['Credit'].widget.attrs = {'class': 'form-control',}
        self.fields['To'].widget.attrs = {'class': 'form-control select2',}
        self.fields['By'].widget.attrs = {'class': 'form-control select2',}
        self.fields['narration'].widget.attrs = {'class': 'form-control',}


Multijournal_formSet = inlineformset_factory(
    Multijournaltotal, 
    Multijournal,
    form=MultijournalForm, 
    extra=6,
)

很长时间以来,我在更新inline formset时都遇到了问题。创建视图可以很好地工作,但是当我尝试更新Multijournal中的Multijournaltotal实例时,对象不会显示在表单集中...

我以前尝试过此方法:

class Multijournal_updateview(LoginRequiredMixin,UpdateView):
    model = Multijournaltotal
    form_class  = MultijournaltotalForm
    template_name = 'Multijournal/multi_journal_form.html'


    def get_success_url(self,**kwargs):
        company_details = get_object_or_404(Company, pk=self.kwargs['pk'])
        multijournal_details = get_object_or_404(Multijournaltotal, pk=pk2)
        selectdatefield_details = get_object_or_404(Selectdatefield, pk=self.kwargs['pk3'])
        return reverse(
            'accounting_double_entry:multijournaldetail', 
            kwargs={
                'pk1':company_details.pk,
                'pk2':multijournal_details.pk,
                'pk3':selectdatefield_details.pk
            },
        )



    def get_context_data(self, **kwargs):
        context = super(Multijournal_updateview, self).get_context_data(**kwargs) 
        company_details = get_object_or_404(Company, pk=self.kwargs['pk'])
        context['company_details'] = company_details
        selectdatefield_details = get_object_or_404(Selectdatefield, pk=self.kwargs['pk3'])
        context['selectdatefield_details'] = selectdatefield_details
        multijournal_details = get_object_or_404(Multijournaltotal, pk=self.kwargs['pk2'])
        queryset = Multijournal.objects.filter(total= multijournal_details.id)

        multijournalformset = Multijournal_formSet(self.request.POST or None, queryset=queryset)
        context['multijournalformset'] = multijournalformset


        return context

谁能告诉我我的方向是否正确?

谢谢

0 个答案:

没有答案