Dave Plug
Dave Plug

Reputation: 1079

Using database values in django queryset

Basically what i'm trying to accomplish is a query like this but using QuerySet:

SELECT value1, value2 FROM table WHERE value1 > value2

How do i do this? Thanks for your help.

Upvotes: 0

Views: 49

Answers (2)

Ania Warzecha
Ania Warzecha

Reputation: 1796

You need to use the F() expression

In your example it would look like:

from django.db.models import F
Table.objects.filter(value1__gt=F('value2')).values('value1', 'value2')

Upvotes: 0

johntellsall
johntellsall

Reputation: 15180

Use F expressions to have a query field refer to another.

from django.db.models import F
Entry.objects.filter( n_comments__gt=F('n_pingbacks') )

Upvotes: 1

Related Questions