【问题标题】:The QuerySet value for an exact lookup must be limited to one result using slicing Error in Django使用 Django 中的切片错误,精确查找的 QuerySet 值必须限制为一个结果
【发布时间】:2020-05-05 20:06:18
【问题描述】:

我正在尝试从用户输入他们想要查询的供应商选择(2 个选择)和时间范围(3 个选择)。我同样面临上述错误。下面附上相关文件。

HTML:

<form method="POST" action="/profiles/adminKaLogin/">
            <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" class="custom-control-input" id="defaultInline1" name="inlineDefaultRadiosExample" value="1">
                <label class="custom-control-label" for="defaultInline1">Vendor 1</label>
            </div>

            <!-- Default inline 2-->
            <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" class="custom-control-input" id="defaultInline2" name="inlineDefaultRadiosExample" value="2">
                <label class="custom-control-label" for="defaultInline2">Vendor 2</label>
            </div>
        <br>  
        <h3> Select time period</h3>

            <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" class="custom-control-input" id="defaultInlines1" name="inlineDefaultRadiosExample1" value="1">
                <label class="custom-control-label" for="defaultInlines1">1 Month</label>
            </div>  
            <!-- Default inline 2-->
            <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" class="custom-control-input" id="defaultInlines2" name="inlineDefaultRadiosExample1" value="2">
                <label class="custom-control-label" for="defaultInlines2">2 Months</label>
            </div>
            <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" class="custom-control-input" id="defaultInlines3" name="inlineDefaultRadiosExample1" value="6">
                <label class="custom-control-label" for="defaultInlines3">6 Months</label>
            </div>
            <br>
            <button type="submit" class="btn btn-primary" name="form1">Submit</button>
</form>

models.py

class vendor(models.Model):
    id = models.CharField(max_length=20, primary_key=True)
    name = models.CharField(max_length=30)

class employee(models.Model):
    name = models.OneToOneField(User, on_delete=models.CASCADE)
    id = models.CharField(max_length=20, primary_key=True)
    balance = models.IntegerField(default=0)

class transaction(models.Model):
    vendor_id = models.ForeignKey(vendor, on_delete=models.CASCADE)
    emp_id = models.ForeignKey(employee, on_delete=models.CASCADE)
    debit = models.IntegerField()
    credit = models.IntegerField()
    timestamp = models.DateField(("Date"), default=datetime.date.today)

views.py

if 'form1' in request.POST:
            d={}
            vendor_choice = request.POST["inlineDefaultRadiosExample"]
            date_choice = request.POST["inlineDefaultRadiosExample1"]
            x = employee.objects.all()
            y = vendor.objects.all()
            if date_choice == 1:
                d = transaction.objects.filter(vendor_id=y, emp_id = x, timestamp__gte = datetime.date.today() - datetime.timedelta(days=30))
            elif date_choice == 2:
                d = transaction.objects.filter(vendor_id=y, emp_id = x, timestamp__gte = datetime.date.today() - datetime.timedelta(days=60))
            else:
                d = transaction.objects.filter(vendor_id=y, emp_id = x, timestamp__gte = datetime.date.today() - datetime.timedelta(days=180))    
            print(d)
            return render(request, 'profiles/adminKaLogin.html', {'model':d})     

我想知道为什么会出现这个问题。任何帮助表示赞赏。

【问题讨论】:

    标签: django forms django-models django-views


    【解决方案1】:

    如果使用list/tuple/queryset进行过滤,需要在=之前使用__in

    观看documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 2020-11-13
      相关资源
      最近更新 更多