Как сохранить изменения, сделанные в inlineformset_factory в UpdateView, не удаляя предыдущие данные? - PullRequest
0 голосов
/ 01 апреля 2019

Я хочу обновить изменения, внесенные в inlineformset_factory, а не удалять их, добавив новое.

models.py

class invoice(models.Model):
    invoice_no = models.CharField(max_length=20)
    invoice_date = models.DateField(auto_now=False, auto_now_add=False)


class invoice_description(models.Model):
    invoice = models.ForeignKey(invoice, related_name='items',
                            unique=False, on_delete=models.CASCADE)
    service_description = models.CharField(max_length=50, blank=False)
    hsn = models.CharField(max_length=50, blank=False)
    qty = models.IntegerField(blank=False)
    per = models.CharField(max_length=50, blank=False)
    rate = models.IntegerField(blank=False)
    currency = models.CharField(max_length=50, blank=False)
    ex_rate = models.IntegerField(blank=False)
    amount = models.IntegerField(blank=False)
    gst = models.IntegerField(blank=False)

forms.py

class InvoiceForm(forms.ModelForm):
    class Meta:
        model = invoice
        fields = ['invoice_no', 'invoice_date']
        widgets = {
        'invoice_no': forms.TextInput(attrs={'class': 'form-control'}),
        'invoice_date': forms.DateInput(attrs={'type': 'date', 'class': 'form-control'}, format="%Y-%m-%d"),
        }


class InvoiceDesForm(forms.ModelForm):
    class Meta:
        model = invoice_description
        fields = '__all__'
        widgets = {
                'service_description': forms.TextInput(attrs={'size': 20}),
                'hsn': forms.TextInput(attrs={'size': 5}),
                'qty': forms.NumberInput(attrs={'class': 'form-number1'}),
                'per': forms.TextInput(attrs={'size': 5}),
                'currency': forms.TextInput(attrs={'size': 5}),
                'rate': forms.NumberInput(attrs={'class': 'form-number1'}),
                'ex_rate': forms.NumberInput(attrs={'class': 'form-number1'}),
                'amount': forms.TextInput(attrs={'size': 5}),
                'gst': forms.NumberInput(attrs={'class': 'form-number'}),
                }

InvoiceFormSet = inlineformset_factory (invoice, invoice_description, form = InvoiceDesForm, extra = 5, can_delete = True)

views.py

class InvoiceUpdateView(UpdateView):
    template_name = 'home/invoice_form.html'
    model = invoice
    form_class = InvoiceForm

    def get_success_url(self):
        self.success_url = 'home/invoice_form.html'
        return self.success_url

    def get_context_data(self, **kwargs):
        context = super(InvoiceUpdateView, self).get_context_data(**kwargs)
        if self.request.POST:
        context['form'] = InvoiceForm(self.request.POST, instance=self.object)
        context['invdescription_form'] = InvoiceFormSet(self.request.POST,
                                                        instance=self.object)
    else:
        context['form'] = InvoiceForm(instance=self.object)
        context['invdescription_form'] = InvoiceFormSet(instance=self.object)
    return context

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        qs = invoice_description.objects.filter(invoice=self.get_object())
        invdescription_form = InvoiceFormSet(self.request.POST, queryset=qs)
        if (form.is_valid() and invdescription_form.is_valid()):
            return self.form_valid(form, invdescription_form)
        else:
            return self.form_invalid(form, invdescription_form)

    def form_valid(self, form, invdescription_form, *args, **kwargs):
        form.save()    
        invdescription_form.instance = self.object
        invdescription_form.save()
        return HttpResponseRedirect(self.get_success_url())

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

Я хочу обновить изменения, сделанные в formset, не удаляяэто.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...