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

Lookup function in Q expression #7

Closed
highpost opened this issue Jun 29, 2020 · 4 comments
Closed

Lookup function in Q expression #7

highpost opened this issue Jun 29, 2020 · 4 comments

Comments

@highpost
Copy link

I'm new to django-model-values and I really think it will be worth the time to learn it well. I especially like the pandas-like notation. Right now I'm trying to understand understand Lookup functions.

Consider the following Q expression:

Q(sales__gt = 100.00)

Now I would like to use round to round the sales value (which is a float) to the nearest penny. Is that even possible? I've looked at the examples in test_all.py, but I'm still in a fog.

Thanks.

@coady
Copy link
Owner

coady commented Jun 30, 2020

Yes, Round can be used as a function, or as a lookup. But to use it as a lookup, it has to registered as a transform with FloatField as described in that link.

In [1]: from model_values import F                                                                                                                                                                                                              

In [2]: round(F.sales)                                                                                                                                                                                                                          
Out[2]: Round(F(sales))

In [3]: F.sales.round > 100.0                                                                                                                                                                                                                   
Out[3]: <Q: (AND: ('sales__round__gt', 100.0))>

@coady coady closed this as completed Aug 5, 2020
@auvipy
Copy link

auvipy commented Jan 21, 2021

Yes, Round can be used as a function, or as a lookup. But to use it as a lookup, it has to registered as a transform with FloatField as described in that link.

In [1]: from model_values import F                                                                                                                                                                                                              

In [2]: round(F.sales)                                                                                                                                                                                                                          
Out[2]: Round(F(sales))

In [3]: F.sales.round > 100.0                                                                                                                                                                                                                   
Out[3]: <Q: (AND: ('sales__round__gt', 100.0))>

can we add this to documentation?

@coady
Copy link
Owner

coady commented Jan 23, 2021

Added in 3115244.

@auvipy
Copy link

auvipy commented Jan 23, 2021

thanks!

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

3 participants