简体   繁体   English

Django CreateView内联表单集实例和queryset

[英]Django CreateView inline formset instance and queryset

So I have a view to make customer payments. 因此,我有一种付款方式。 When the page is first rendered there's an empty form. 首次呈现页面时,会有一个空白表格。 I'm using ajax to choose a customer then return an html string of all the outstanding invoices for that customer and inserting it into the empty form. 我正在使用ajax选择客户,然后返回该客户所有未清发票的html字符串,并将其插入空白表格。

When the form is submitted in order for the outstanding invoices formset to be valid I need to pass it the matching instance and queryset as the ajax generated formset. 当提交表单以使未付发票表单集有效时,我需要将匹配的实例和queryset作为ajax生成的表单集传递给它。 The problem I'm running into is passing the customer instance into get_context_data(). 我遇到的问题是将客户实例传递给get_context_data()。

Right now I have customer hard-coded to pk=1 and can confirm that the formset is valid. 现在,我已经将客户硬编码为pk = 1,并且可以确认该表单集有效。 There's also a hidden field with the value for customer in the form but I'm not sure how to pass that into get_context_data(). 表单中还有一个隐藏的字段,其中包含用于客户的值,但是我不确定如何将其传递给get_context_data()。

Anyone have any idea how to accomplish this? 任何人都知道如何做到这一点?

class PaymentCreate(CreateView):
    template_name = 'payment_create.html'
    model = Pay_hdr
    success_url = reverse_lazy('payments')
    form_class = PaymentHeadForm

    def get_context_data(self, **kwargs):
        context = super(PaymentCreate, self).get_context_data(**kwargs)
        context['customers'] = Customer.objects.all().order_by('name')
        if self.request.POST:
            customer = Customer.objects.get(pk=1)  # need to replace this line
            queryset = Accounts_receivable.objects.filter(customer=customer).exclude(balance=0).order_by('-date_trans')
            context['user'] = self.request.user
            context['formset'] = ARLineFormset(self.request.POST, instance=customer, queryset=queryset)
        else:
            context['formset'] = ARLineFormset()
        return context

    def form_valid(self, form):
        context = self.get_context_data()
        user = context['user']
        formset = context['formset']
        if form.is_valid() and formset.is_valid():
            head = form.save(commit=False)
            invoices = formset.save(commit=False)
            head.created_by = user
            head.save()
            for invoice in invoices:
                if invoice.temp_amount > 0:
                    invoice.process_payment(head, invoice.temp_amount, user)
            return redirect(self.success_url)
        else:
            return render(self.request, self.template_name, self.get_context_data(form=form))

def ajax_payment_choose_customer(request):
    customer_id = request.GET.get('customer_id', None)
    customer = Customer.objects.get(pk=customer_id)
    outstanding = Accounts_receivable.objects.filter(customer=customer).exclude(balance=0).order_by('-date_trans')
    if request.is_ajax():
        form = PaymentHeadForm()
        formset = ARLineFormset(instance=customer, queryset=outstanding)
        context = {
            'customer': customer,
            'form': form,
            'outstanding': outstanding,
            'formset': formset
        }
        html = render_to_string('payment_insert_forms.html', context)
        return HttpResponse(html)

Replace customer = Customer.objects.get(pk=1) 替换customer = Customer.objects.get(pk=1)

with customer = Customer.objects.get(pk=self.request.POST.get('customer')) customer = Customer.objects.get(pk=self.request.POST.get('customer'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM