FAILED TO SAVE DJANGO INLINE FORMSET WITH HTML INPUT TO DB

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 class="container1" style="width: 125%;">
        <form class="/purchase_order_list" method="POST" action="">{% csrf_token %}
        
                    <div class="column is-one-third" style="line-height: 10px;">                         
                        <div class="field" style="margin-left: 85.7%; width: 150px; margin-top: 6%;"><br>
                            <label>Lpo No.</label>
                            <div class="control">
                            <input class="input form-control" value="{{ form_lpo.lpo_number }}">
                            </div>
                          
                        </div>  
                    </div>
                    <div class="column is-one-third">                         
                        <div class="field" style="margin-left: 85.7%; width: 150px; margin-top: 0.2%;">
                            <label>Grn No.</label>
                            <div class="control">
                             <input class="input form-control" value="{{ form_lpo.grn_number }}">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="columns is-centered">
                <div class="column is-one-third">                         
                    <div class="col-md-4 mb-3">
                        <label>Vendor Name</label>
                        <div class="control">
                         <input class="input form-control" value="{{ form_lpo.vendor_name }}">
                        </div>
                    </div>
                </div>
                <div class="column is-one-third">
                    <div class="col-md-4 mb-3">
                        <label>Invoice Number</label>
                        <div class="control">
                             <input class="input form-control" value="{{ form_lpo.vendor_invoice_number }}">
                        </div>
                    </div>
                </div>
                <div class="column is-one-third">                   
                    <div class="col-md-4 mb-3">
                        <label>Descriptions</label>
                        <div class="control">
                         <input class="input form-control" value="{{ form_lpo.descriptions }}">
                        </div>
                    </div>
                </div>
            </div>

            <div class="columns is-centered">
                <div class="column is-one-third">
                    <div class="col-md-4 mb-3">
                        <label>Means of Quote</label>
                            <input class="input form-control" value="{{ form_lpo.means_of_quote}}">
                    </div>
                </div>
                <div class="column is-one-third">         
                    <div class="col-md-4 mb-3">
                        <label>VAT</label>
                        <div style="display: flex;">
                             <input class="input form-control" value="{{ form_lpo.vat }}">
                        </div>
                    </div>
                </div>
                <div class="column is-one-third">                   
                    <div class="col-md-4 mb-3">
                        <label>Currency</label>
                        <input class="input form-control" value="{{ form_lpo.currency }}">
                    </div>
                </div>
            </div>
            
            <div class="columns is-centered">
                <div class="column is-one-third">
                    <div class="col-md-4 mb-3">
                        <label>Address</label>
                        <div class="control">
                             <input class="input form-control" value="{{ form_lpo.address }}">
                        </div>
                    </div>
                </div>
                <div class="column is-one-third">                         
                    <div class="col-md-4 mb-3">
                        <label>Date</label>
                        <div class="control">
                         <input class="input form-control" value="{{ form_lpo.date }}">
                        </div>
                    </div>
                </div>
            </div>
            <table class="table is-fullwidth is-bordered is-hoverable is-striped" 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 class="input form-control" value="{{ formset.type_of_service }}"></td>
                            <td><input class="input form-control" value="{{ formset.item }}"></td>
                            <td><input class="input form-control" value="{{formset.quantity_remaining}}"></td>
                            <td><input class="input form-control" value="{{formset.quantity_to_receive}}"></td>
                            <td><input class="input form-control" value="{{ formset.rate }}"></td>
                            <td><input class="input form-control" value="{{formset.amount}}"></td>
                        </tr>   
                        {% endfor %}
                        </tbody>              
                </table>
           <!-- </div>
                <div class="col-4 offset-4">
                    <input type="button" class="btn-block is-info is-half" value="Show amount received">
                </div>-->
                <div class="columns is-centered">
                    <div class="column is-one-third">
                        <div class="col-md-4 mb-3">
                            <label>Grand Total</label>
                            <div class="input">
                                 {{form.grn_amount}}
                            </div>
                        </div>
                    </div>
                    <div class="column is-one-third">                         
                        <div class="col-md-4 mb-3">
                            <label>Vat</label>
                            <div class="input">
                             {{form.grn_vat}}
                            </div>
                        </div>
                    </div>
                    <div class="column is-one-third">                   
                        <div class="col-md-4 mb-3">
                            <label>Total Amount</label>
                            <div class="input">
                             {{form.total_grn}}
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row spacer" style="float: right;">
                    <div class="col-4 offset-2">
                        <button type="submit" class="button btn btn-block is-info is-half save" 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

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!

@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 class="form-control input" value="{{ lpo_line_item.type_of_service }}"></td>

Back to Top