I have a queryset that only needs to be filtered in certain ways based on specific user provided conditions.
# construct the base queryset first
queryset = Spot.objects.values()
# zero is the default value,
# so only need to filter if an actual value is provided
if mindist > 0:
queryset = queryset.filter(distance >= mindist)
if maxdist > 0:
queryset = queryset.filter(distance <= maxdist)
if starttime != 0:
queryset = queryset.filter(unix_time >= starttime)
if endtime != 0:
queryset = queryset.filter(unix_time <= endtime)
objects_list = list(queryset)
return objects_list
Essentially, I am trying to replicate the functionality mentioned in the Django docs https://docs.djangoproject.com/en/1.8/topics/db/queries/#querysets-are-lazy
but with conditions and inside a .py script (instead of the interpreter), but I am getting a 'Name is not defined' error. A few searches indicates that it might be a declaration error with
queryset = Spot.objects.values()
So I've tried it with other variants like
queryset = Spot.objects.filter()
queryset = Spot.objects.all()
queryset = Spot.objects()
but none of them seems to work.
EDIT : Here is my models.py
class Spot(models.Model):
spot_id = models.IntegerField(db_column='Spot_ID', primary_key=True) # Field name made lowercase.
unix_time = models.IntegerField(db_column='UNIX_time', blank=True, null=True) # Field name made lowercase.
distance = models.IntegerField(db_column='Distance', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'SPOT'
To clarify further. The error comes up for whichever filter that comes right after the initial Spot.objects.values(). For example in the one above,
queryset = queryset.filter(distance >= mindist)
is the first one, and the error will point to that line with 'distance' not defined . I've tried switching the order around eg putting
queryset = queryset.filter(unix_time <= endtime)
first instead. but I get the same error, but this time with 'unix_time' not defined .
I've double checked the names and they are all correct. I'm using a
from .models import *
to import everything, because I also have queries in the script that hits the same database (but different tables) that works perfectly fine, so I'm relatively sure it's not an import issue.
您需要将queryset = Spot.objects
而不是queryset = Spot.objects.values()
去,以防您导入Spot model
。
SOLVED
So I finally figured out what was wrong. Apparently, filter() does not recognize the standard comparison operators like >= and <=.
Instead it uses Field Lookups to emulate SQL WHERE clauses. So instead of
queryset = queryset.filter(unix_time >= starttime)
I should be doing
queryset = queryset.filter(unix_time___gte = starttime)
Thanks to everyone who tried to help, and I hope this will be useful to anyone who encounters the same problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.