Björn
Björn

Reputation: 67

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?

Upvotes: 0

Views: 1848

Answers (4)

lastmaj
lastmaj

Reputation: 336

Use __gt instead of > :

result = Basestationreport.objects.filter(gpstimeanddate__gt = datetime.date(2005, 1, 1))

All of the docs links given above are broken, here's an updated link : __gt

Upvotes: 0

alexpeits
alexpeits

Reputation: 877

This line:

result = Basestationreport.objects.filter(gpstimeanddate > datetime.date(2005, 1, 1))

Should be

result = Basestationreport.objects.filter(gpstimeanddate__gt=datetime.date(2005, 1, 1))

Django queries like filter only accept keyword arguments, not boolean expressions. Check out this link, and generally the docs.

Upvotes: 1

Siva Arunachalam
Siva Arunachalam

Reputation: 7750

_gt should be used instead of >. You can read more from this documentation.

result = Basestationreport.objects.filter(gpstimeanddate__gt = datetime.date(2005, 1, 1))

Upvotes: 1

Sayse
Sayse

Reputation: 43320

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))

Upvotes: 1

Related Questions