Today was added a new feature to django ORM. Now it supports references to fields in filters expressions. Just checkout the SVN version and enjoy it.
Before this update, queryset could be filtered only by absolute values.
queryset.filter(field='absolute value')
Will generate somthing like this:
SELECT * FROM table_name WHERE field = 'absolute value';
Now you can filter by another’s table field value.
queryset.filter(field=F('another_field'))
Will generate something like this:
SELECT * FROM table_name WHERE field = another_field;
The feature also supports arithmetical operations in the lookups so you can do filters where the views_number is twice as big as the comments_number.
queryset.filter(views_number__gt=F('comments_number')*2)
Will generate a SQL like this:
SELECT * FROM table_name WHERE views_number = comments_number*2;
It’s a very good feature. So, checkout the SVN version and take a look at docs/topics/db/queries.txt to know more about this.
