django: Filter by field value in row on model

A bit hard to describe in words, but easier in an example.

Say for instance I have these values in my database table.

id | rowA | rowB
----------------
1  | 3    | 1
2  | 3    | 2
3  | 3    | 3
4  | 3    | 4
5  | 3    | 5

To find the rows where rowA >= rowB, you just use the F class.

from django.db.models import F
Model.objects.filter(rowA__gte=F('rowB'))

So easy but never crossed this before!

0D00v

Source

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog