user3592255 user3592255 - 5 months ago 21
Python Question

Django model is present, says not defined when running code

i have the following model:

class Basestationreport(models.Model):
...
gpstimeanddate = models.DateTimeField(db_column='GPSTimeAndDate') # Field name made lowercase.
...


And code:

from .models import Basestationreport

@login_required(login_url="login/")
def getbasestationsbetween(request):
fromdate = request.POST.get('from')
todate = request.POST.get('to')
result = Basestationreport.objects.filter(gpstimeanddate > datetime.date(2005, 1, 1))
return HttpResponse(json.dumps(request.POST), content_type="application/json")


The error i get is:


NameError: name 'gpstimeanddate' is not defined.


My other functions which use other fields from this table work fine. Am I missing something?

Answer

The error is because its looking for a local variable called gpstimeanddate which you obviously haven't defined, nor are trying to use.

Since you're trying to use it as a field to filter results on you need to use it as a keyword in fiter, as well as use __gt

.filter(gpstimeanddate__gt=datetime.date(2005, 1, 1))