【问题标题】:How to auto calculate fields in django?如何在 django 中自动计算字段?
【发布时间】:2019-09-10 22:41:46
【问题描述】:

这是我的模型:

class Stockdata(models.Model):
    quantity    = models.PositiveIntegerField(null=True,blank=True)
    rate        = models.DecimalField(max_digits=10,decimal_places=2,default=0.00)
    opening     = models.DecimalField(max_digits=10,decimal_places=2,default=0.00)
    stock_name  = models.CharField(max_length=32)

我想做类似的事情: opening = quantity * rate 在我的 django 表单模板中。

我尝试了以下方法:

<script type="text/javascript">

$(document).ready(function() {
    $('#id_Quantity').keyup(function() {
        var a = $('#id_rate').val();
        var b = $(this).val();
        $('#id_opening').val(a * b);
    });
});

</script>

但这并不是没有给出结果

我的模板:

            <form method="POST">

      <div class="form-group row">

                <label class="col-lg-2 col-form-label">Stock Name<i class="material-icons" style="font-size:16px;color:red">*</i></label>

                    <div class="col-lg-10">
                    {{ form.stock_name.errors }}
                        {{ form.stock_name }}
                    </div>
                </div>
           <div class="form-group row">

                <label class="col-lg-2 col-form-label">Quantity<i class="material-icons" style="font-size:16px;color:red">*</i></label>

                    <div class="col-lg-10">
                    {{ form.Quantity.errors }}
                    {{ form.Quantity }}
                    </div>
                </div>

                <div class="form-group row">

                <label class="col-lg-2 col-form-label">Rate<i class="material-icons" style="font-size:16px;color:red">*</i></label>

                    <div class="col-lg-10">
                    {{ form.rate.errors }}
                    {{ form.rate }}
                    </div>
                </div>

                <div class="form-group row">

                <label class="col-lg-2 col-form-label">Opening Balance<i class="material-icons" style="font-size:16px;color:red">*</i></label>

                    <div class="col-lg-10">
                    {{ form.opening.errors }}
                    {{ form.opening }}
                    </div>
                </div>
            </form>

有人知道如何执行此操作吗? 实际上,我对 jquery 非常陌生,因此遇到了一些基本问题。 谢谢

【问题讨论】:

    标签: jquery django


    【解决方案1】:

    我建议不要在前端进行计算,将数据发布到后端(除非您需要向用户显示opening的值)。

    相反,您应该在Stockdata django 模型中计算保存时的opening 值,只需覆盖save 方法即可。如下所示。

    class Stockdata(models.Model):
        quantity    = models.PositiveIntegerField(null=True,blank=True)
        rate        = models.DecimalField(max_digits=10,decimal_places=2,default=0.00)
        opening     = models.DecimalField(max_digits=10,decimal_places=2,default=0.00)
        stock_name  = models.CharField(max_length=32)
    
        # you override the save method and calculate for opening
        def save(self, *args, **kwargs):
            self.opening = self.quantity * self.rate;
            super(Stockdata, self).save(*args, **kwargs)
    

    注意 如果您在用户输入数量值和费率值时仍需要向用户显示期初值,则需要对脚本进行一些修改:

    <script type="text/javascript">
      $(document).ready(function() {
        $('#id_Quantity').keyup(function() {
            var rate = parseFloat($('#id_rate').val());
            var quantity = parseInt($(this).val(), 10);
            $('#id_opening').text(rate * quantity);
        });
      });
    </script>
    

    那么您的 html 部分将是:

    <form method="POST">
    
      <div class="form-group row">
    
        <label class="col-lg-2 col-form-label">Stock Name<i class="material-icons"
            style="font-size:16px;color:red">*</i></label>
    
        <div class="col-lg-10">
          {{ form.stock_name.errors }}
          {{ form.stock_name }}
        </div>
      </div>
      <div class="form-group row">
    
        <label class="col-lg-2 col-form-label">Quantity<i class="material-icons"
            style="font-size:16px;color:red">*</i></label>
    
        <div class="col-lg-10">
          {{ form.Quantity.errors }}
          {{ form.Quantity }}
        </div>
      </div>
    
      <div class="form-group row">
    
        <label class="col-lg-2 col-form-label">Rate<i class="material-icons" style="font-size:16px;color:red">*</i></label>
    
        <div class="col-lg-10">
          {{ form.rate.errors }}
          {{ form.rate }}
        </div>
      </div>
    
      <div class="form-group row">
    
        <label class="col-lg-2 col-form-label">Opening Balance<i class="material-icons"
            style="font-size:16px;color:red">*</i></label>
    
        <div class="col-lg-10">
          <!-- opening value will be displayed here -->
          <label id="id_opening"></label>
        </div>
      </div>
    </form>
    

    【讨论】:

    • 它不工作...我试过...结果来了0
    • 您是否从数据库中获取0 结果?
    【解决方案2】:

    有一个 django 应用,通过在表单小部件的定义中编写表达式来方便前端的计算。

    django-calculation您可以通过以下方式获得它:

    pip install django-calculation
    

    一个如何通过在 ModelForm 中定义表达式来实现计算的示例。

    from django import forms
    
    # import your Stockdata model
    from .models import Stockdata
    
    # import the calculation module from django-calculation app
    import calculation
    
    # create a ModelForm of you Stockdata model
    class StockdataForm(forms.ModelForm)
        class Meta:
            model = Stockdata
            fields = ['quantity', 'rate', 'opening', 'stock_name']
            # define a custom widget to your opening field
            widgets = {
                'opening': calculation.FormulaInput('rate*quantity')
            }
            
    

    确保在您的模板中包含{{ form.media }}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 2010-09-29
      • 2021-05-06
      • 1970-01-01
      相关资源
      最近更新 更多