0

I'm going through an example where a Model has a DateTimeField defined upon it. When trying to create a filtered QuerySet with a date lookup, the following error is returned. I'm confused as to why this error is being raised. It makes it sound like a function that I wrote is causing the error (which is not the case), but perhaps I'm reading that incorrectly.

django.db.utils.OperationalError: user-defined function raised exception

>>> a = Employee.objects.create(
  alias="User", age=0, hired=datetime(2020, 6, 12), experience=0
)
>>> result = Employee.objects.filter(hired__date__gt=datetime(2019, 1, 1))
from django.db import models

class Employee(models.Model):
    alias = models.CharField(unique=True, max_length=11)
    age = models.IntegerField()
    hired = models.DateTimeField()
    experience = models.IntegerField()

    def __str__(self):
        return self.alias

1 Answer 1

1

It should be,

result = Employee.objects.filter(hired__date__gte=datetime.date(2019, 1, 1))

Sign up to request clarification or add additional context in comments.

2 Comments

What purpose would the date lookup serve then? docs.djangoproject.com/en/3.0/ref/models/querysets/#date For datetime fields, casts the value as date. Allows chaining additional field lookups. Takes a date value.
Updated answer. if you want to use date lookup you yourself referenced to right docs. Note I added .date method at end. Docs refer them with date lookup, it separates and removes time from date. I hope you understand ask anything if want too I'll clear you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.