On Fri, 2009-08-07 at 06:15 -0700, talpay...@gmail.com wrote: > i have come up with one of the most interesting query i have ever > seen. > > I want to do this : > > products = products.filter(productdetail__detail_value__gte = > first_value) > products = products.filter(productdetail__detail_value__lte = > second_value) > > where first_value and second_value are integers and > "productdetail__detail_value" are stored as a char. > > it is a very simple sql query but how can i do this using django > queryset. the problem is that i need the products to stay as a > queryset... because i need to filter it again if that is the case.
So what is the problem you are seeing? At the end of the above two statements, "product" is a queryset, so that part of your problem is solved. > > taking the easy way out and changing the way the table is constructed > is not an option. i have to store any kind of data in "detail_value" > and filter the information. it's a generic filter and this is the only > problem i don't have a solution to..... please help. You have a modelling problem, in that you're using "<=" on a character field, which doesn't make sense for generic data. That's not something Django can help you with out of the box. You say you can't change the field type to something numeric type because have to store "anything" in that field, but what do you expect to happen in the filter if detail_value is "elephant", say? It is neither less than, nor greater than, nor equal to a number. It's incomparable. So you're asking Django to read your mind a bit there. Since you're wanting to do something that is highly SQL specific here, you'll want to first work out what the query will look like in raw SQL. I suspect there's going to be either a nested query to only extract numeric values or maybe a coallesce statement or maybe something else. I can't visualise how you might do this, since it's very data dependent. Work out the SQL query directly, perhaps on a simplified model and then let us know what that might be and then we might be able to help convert it into something you can use in the ORM. Or maybe not -- there are (intentionally) limits on what you can do directly in the ORM and the answer for edge-cases is often to write the query directly in SQL. Which means you cannot use it further as a queryset (you could create something that could be used to extra results, easily enough, but further filter() calls, etc, are much harder to inject correctly). Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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 -~----------~----~----~----~------~----~------~--~---