Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to combine with Extract function #29

Open
sevdog opened this issue Oct 12, 2022 · 1 comment
Open

Unable to combine with Extract function #29

sevdog opened this issue Oct 12, 2022 · 1 comment

Comments

@sevdog
Copy link

sevdog commented Oct 12, 2022

When using this field is not possible to direcly use the Django ORM function Extract (and similars).

Since this function checks field type before passing to database:

ValueError: Extract input expression must be DateField, DateTimeField, TimeField, or DurationField.

This does not allow the developer to use with ease the postgres DB function EXTRACT.

@sevdog
Copy link
Author

sevdog commented Oct 12, 2022

A workaround for this is to use ExpressionWrapper:

from django.db.models import  DurationField, ExpressionWrapper, F 
from django.db.models.functions import Extract

MyModel.objects.annotate(
  epoch=Extract(
    ExpressionWrapper(F('myfield'), DurationField()),
    'epoch',
  ),
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant