On Tue, Jan 12, 2010 at 01:40:50PM -0800, Daniel Roseman wrote:
> On Jan 12, 9:29 pm, Ryan Nowakowski <tuba...@fattuba.com> wrote:
> > I have a model that has an IntegerField.  I'd like to filter only the
> > even integers.  sqlite has a modulus operator so I can do:
> >
> >   select * from mytable where my_int = my_int % 2
> 
> Er, that doesn't give you even integers, it gives you rows where
> my_int is equal to the remainder when my_int is divided by 2 (see
> http://www.sqlite.org/lang_expr.html) In other words, it will only
> ever return rows where my_int = 1.
> 
> I suspect you mean 'where my_int % 2 = 0'.
> 
> > However, I'd like to use the QuerySet API to do this instead of
> > resorting to SQL.  Any ideas or hints?
> >
> > Thanks,
> >
> > Ryan
> 
> You can use the extra() method:
> 
> MyTable.objects.extra(where=['my_int % 2 = 0'])
> --
> DR.

Yup, that's right.  Although, FYI I have to use a double percent sign
so python won't see it as a string parameter:

MyTable.objects.extra(where=['my_int %% 2 = 0'])
-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to