Re: Filter to find closest match

2007-07-30 Thread Michael
OK, sorted using: object=pricelist.pricelisttable_set.extra(where=["""(abs(width - %s)) = ( SELECT Min(Abs(pl.width - %s)) FROM table_pricelisttable pl where name_id=%s)"""], params=[w, w,d.pricelist_id])[0] The error was missing in subquery where name_id=%s. Now it works exactly as should.

Re: Filter to find closest match

2007-07-30 Thread Michael
Yes, you've right. If I put = the query will return None ! The only query which return what i want is only raw sql: select min(abs(width-240)) as m,width from table_pricelisttable where name_id = 1 group by width order by m limit 1; than performing the second query to get object. like: cursor

Re: Filter to find closest match

2007-07-30 Thread Tim Chase
> Seems to work now with: > w=240 > pricelisttable_set.extra(where=["""(table_pricelisttable.width - %s) > >= ( SELECT Min(Abs(pl.width - %s)) FROM table_pricelisttable pl) > """], params=[w, w])[0] I would be very surprised if it works now as described with ">=" rather than "=". It's basically

Re: Filter to find closest match

2007-07-30 Thread Michael
Thank you Tim, Seems to work now with: w=240 pricelisttable_set.extra(where=["""(table_pricelisttable.width - %s) >= ( SELECT Min(Abs(pl.width - %s)) FROM table_pricelisttable pl) """], params=[w, w])[0] On 7/29/07, Tim Chase <[EMAIL PROTECTED]> wrote: > > > In pure sql we can do : > >

Re: Filter to find closest match

2007-07-29 Thread Tim Chase
> In pure sql we can do : > "select min(abs(width-150)) from quote_pricelisttable where name_id = > 1 and width >= 240-150 and width <=240+150;" > > How to make it work properly from django ORM? Did you try playing with the extra() call I included in my previous email? It should do what you

Re: Filter to find closest match

2007-07-29 Thread Michael
On 7/29/07, Tim Chase <[EMAIL PROTECTED]> wrote: > > > currently I using filter to find closest match: > > step=100 > > w=2000 # get it from forms. > > value=pricelist.pricelisttable_set.filter(width__gte=w-step,width__lte=w+step)[0] > > > > The problem is that "step" is different for different