如何使用filter()函数获取范围之间的数据?

2024-07-04 13:40:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我写下了这样一个模型:

型号.py

from __future__ import unicode_literals
from django.db import models

# Create your models here.
class  SequenceDetails(models.Model):

    IDs = models.CharField(max_length=30)
    country = models.CharField(max_length=30)
    year = models.IntegerField(max_length=30)

    def __unicode__(self):

        return self.IDs 

为了从数据库中获取数据,我写下如下查询:

SequenceDetails.objects.all().filter(country='india').filter(year='2002')

这是返回预期的结果,但进一步我想过滤出数据的基础上,国家和年份范围。我写了一个这样的查询集。你知道吗

SequenceDetails.objects.all().filter(country='india').filter(year__range =['2002','2005'])

但它现在的工作和抛出错误如下:

SyntaxError: invalid syntax

我怎样才能做到这一点谢谢


Tags: fromimportselfidsobjectsmodelsunicodeall
2条回答

这是答案

SequenceDetails.objects.all().filter(country='india',year__in=[2002,2000])

年份是整数字段,因此需要在关键字中使用

尝试以下解决方案

SequenceDetails.objects.filter(country='india', year__lte=2005, year__gte=2002])

参考gte/ltedjango文件。你知道吗

希望这有帮助。你知道吗

相关问题 更多 >

    热门问题