无法将Django inline formset with html输入保存到数据库

xienkqul  于 2023-01-27  发布在  Go
关注(0)|答案(2)|浏览(105)

我尝试将数据从Purchase Order Model返回到Grn Form,我想将其保存到Grn Model,但无法保存。以下是返回数据但无法保存到数据库的视图类和grn表单

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模板

<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>

当我尝试使用替代手段,如函数视图获取数据,但只保存表单,忽略表单集

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)

请帮忙解决,我还是乞丐.谢谢

1zmg4dgp

1zmg4dgp1#

检查这部分代码:

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()

有一个机会的变量之一总是空的,没有任何形式保存!也许!

yv5phkfx

yv5phkfx2#

@babak,我尝试使用formset本身而不从db返回数据,在模板formset中我这样做了

{{ 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 %}

它工作正常,我可以猜测问题是如何保存表单集与html输入<td><input class="form-control input" value="{{ lpo_line_item.type_of_service }}"></td>

相关问题