Home > Blockchain >  Failed to save Django inline formset withhtml input to db
Failed to save Django inline formset withhtml input to db

Time:01-22

I tried to return data from Purchase Order Model to Grn Form and i want to save it in Grn Model but i cant save it. Here is my view class and grn form which return data but cant save to db

class view.py

class CreateGrn(LoginRequiredMixin, CreateView):
    model= GrnModel
    template_name = "inventory/grn_create.html"

class GrnCreate(LoginRequiredMixin, CreateView):
    model = GrnModel
    template_name = "inventory/grn_create.html"
    form_class = GrnForm
    success_url = reverse_lazy("/")

    def get_context_data(self, **kwargs):
        data = super(GrnCreate, self).get_context_data(**kwargs)
        if self.request.POST:
            data['formset'] = Grn_LineItemFormset(self.request.POST)
        else:
            data['formset'] = Grn_LineItemFormset()
            data['grn_no'] = GrnModel.objects.filter(company_grn = self.request.user.first_name).order_by('-id')[:1]
            pk = self.kwargs["pk"]
            data['form_lpo'] = PurchaseOrderModel.objects.get(pk = pk)
            #data['lpo_line_item'] = data['form_lpo'].purchaseorder_lineitem_set.all()
        return data

    def form_valid(self, form):
        context = self.get_context_data()
        formset = context['formset']
        with transaction.atomic():
            form.instance.grn_user = self.request.user
            self.object = form.save()

            if formset.is_valid():
                formset.instance = self.object
                formset.save()
        return super(GrnCreate, self).form_valid(form)

html template

<div  style="width: 125%;">
        <form  method="POST" action="">{% csrf_token %}
        
                    <div  style="line-height: 10px;">                         
                        <div  style="margin-left: 85.7%; width: 150px; margin-top: 6%;"><br>
                            <label>Lpo No.</label>
                            <div >
                            <input  value="{{ form_lpo.lpo_number }}">
                            </div>
                          
                        </div>  
                    </div>
                    <div >                         
                        <div  style="margin-left: 85.7%; width: 150px; margin-top: 0.2%;">
                            <label>Grn No.</label>
                            <div >
                             <input  value="{{ form_lpo.grn_number }}">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div >
                <div >                         
                    <div >
                        <label>Vendor Name</label>
                        <div >
                         <input  value="{{ form_lpo.vendor_name }}">
                        </div>
                    </div>
                </div>
                <div >
                    <div >
                        <label>Invoice Number</label>
                        <div >
                             <input  value="{{ form_lpo.vendor_invoice_number }}">
                        </div>
                    </div>
                </div>
                <div >                   
                    <div >
                        <label>Descriptions</label>
                        <div >
                         <input  value="{{ form_lpo.descriptions }}">
                        </div>
                    </div>
                </div>
            </div>

            <div >
                <div >
                    <div >
                        <label>Means of Quote</label>
                            <input  value="{{ form_lpo.means_of_quote}}">
                    </div>
                </div>
                <div >         
                    <div >
                        <label>VAT</label>
                        <div style="display: flex;">
                             <input  value="{{ form_lpo.vat }}">
                        </div>
                    </div>
                </div>
                <div >                   
                    <div >
                        <label>Currency</label>
                        <input  value="{{ form_lpo.currency }}">
                    </div>
                </div>
            </div>
            
            <div >
                <div >
                    <div >
                        <label>Address</label>
                        <div >
                             <input  value="{{ form_lpo.address }}">
                        </div>
                    </div>
                </div>
                <div >                         
                    <div >
                        <label>Date</label>
                        <div >
                         <input  value="{{ form_lpo.date }}">
                        </div>
                    </div>
                </div>
            </div>
            <table  id="table_field">
                            <thead>
                                <tr style="text-align: left;">
                                   <th>Type</th>
                                   <th>Item</th>
                                   <th>Remaining Qty</th>
                                   <th>Qty to Receive</th>
                                   <th>Rate</th>
                                   <th>Amount</th>
                                   <th></th>
                                </tr>   
                            </thead>
                        <tbody>
                        {{ formset.management_form }}
                        {% for formset in form_lpo.purchaseorder_lineitem_set.all %}
                        <tr>
                            <td><input  value="{{ formset.type_of_service }}"></td>
                            <td><input  value="{{ formset.item }}"></td>
                            <td><input  value="{{formset.quantity_remaining}}"></td>
                            <td><input  value="{{formset.quantity_to_receive}}"></td>
                            <td><input  value="{{ formset.rate }}"></td>
                            <td><input  value="{{formset.amount}}"></td>
                        </tr>   
                        {% endfor %}
                        </tbody>              
                </table>
           <!-- </div>
                <div >
                    <input type="button"  value="Show amount received">
                </div>-->
                <div >
                    <div >
                        <div >
                            <label>Grand Total</label>
                            <div >
                                 {{form.grn_amount}}
                            </div>
                        </div>
                    </div>
                    <div >                         
                        <div >
                            <label>Vat</label>
                            <div >
                             {{form.grn_vat}}
                            </div>
                        </div>
                    </div>
                    <div >                   
                        <div >
                            <label>Total Amount</label>
                            <div >
                             {{form.total_grn}}
                            </div>
                        </div>
                    </div>
                </div>
                <div  style="float: right;">
                    <div >
                        <button type="submit"  style="width: 150px; float: right;">Save</button>
                    </div>
                </div>
           
        </form>
    </div>

When i tried to use alternative means like function view get data but save only form and ignore formset

function view.py

def GrnCreate(request, **kwargs):
    pk = kwargs.get('pk')
    form_lpo = get_object_or_404(PurchaseOrderModel, pk = pk)
    lpo_line_item = form_lpo.purchaseorder_lineitem_set.all()


    if request.method == 'GET':
        formset = Grn_LineItemFormset(request.GET or None)
        form = GrnForm(request.GET or None, instance=form_lpo)
    elif request.method == 'POST':
        formset = Grn_LineItemFormset(request.POST)
        form = GrnForm(request.POST, instance=form_lpo)
        
        if form.is_valid():
            grnmodel= GrnModel.objects.create(vendor_name=form.data["vendor_name"],
                    grn_number=form.data["grn_number"],
                    lpo_number=form.data["lpo_number"],
                    vat=form.data["vat"],
                    address = form.data["address"],
                    date =form.data["date"],
                    vendor_invoice_number = form.data["vendor_invoice_number"], 
                    descriptions=form.data["descriptions"],
                    currency = form.data["currency"],
                    means_of_quote =form.data["means_of_quote"], 
                    grn_amount=form.data["grn_amount"],
                    grn_vat = form.data["grn_vat"],
                    total_grn=form.data["total_grn"],
                    )

        if formset.is_valid():
            for form in formset:
                type = form.cleaned_data.get('type')
                item = form.cleaned_data.get('item')
                quantity_remaining = form.cleaned_data.get('quantity_remaining')
                quantity_to_receive = form.cleaned_data.get('quantity_to_receive')
                rate = form.cleaned_data.get('rate')
                amount = form.cleaned_data.get('amount')
                if type and item and quantity_remaining and quantity_to_receive and rate and amount:
                    Grn_LineItem(vendor_name=grnmodel,
                            type=type,
                            item=item,
                            quantity_remaining=quantity_remaining,
                            quantity_to_receive=quantity_to_receive,
                            rate=rate,
                            amount=amount).save()
            grnmodel.save()
            return redirect('purchase_order_list')  
    context = {
        "formset": formset,
        "form": form,
        "grn_no":grn_no,
        "form_lpo":form_lpo,
        'lpo_line_item':lpo_line_item,
    }
    return render(request, 'inventory/grn_create.html', context)

Please help to solve, am still begginner. Thank you

CodePudding user response:

check this part of the code :

                if type and item and quantity_remaining and quantity_to_receive and rate and amount:
                **strong text**Grn_LineItem(vendor_name=grnmodel,
                        type=type,
                        item=item,
                        quantity_remaining=quantity_remaining,
                        quantity_to_receive=quantity_to_receive,
                        rate=rate,
                        amount=amount).save()

there is a chance one of the variables is always empty that none of forms saving! maybe!

CodePudding user response:

@babak, i tried to use formset itself without return data from db, and in template formset i did this

{{ formset.management_form }}
{% for formset in formset %}<!--form_lpo.purchaseorder_lineitem_set.all %}-->
 <tr>
    <td>{{ formset.type_of_service }}</td>
    <td>{{ formset.item }}</td>
    <td>{{formset.quantity_remaining}}</td>
    <td>{{formset.quantity_to_receive}}</td>
    <td>{{ formset.rate }}</td>
    <td>{{formset.amount}}</td>
   </tr>   
   {% endfor %}

it working fine, i can guess the problem is how to save formset with html input <td><input value="{{ lpo_line_item.type_of_service }}"></td>

  • Related