On Sun, Jan 16, 2011 at 6:51 AM, Mikhail Korobov <kmik...@googlemail.com> wrote: > It doesn't work that way. ORM translates you queries to SQL and the DB > is responsible for filtering. It is not possible to translate > arbitrary python function to SQL or pass it to DB engine. So you have > to formulate you query using .filter syntax or raw SQL.
This is absolutely correct. You can't mix python functions with SQL functions. > > Another possibility is to denormalize data: add 'complete' field to > the model and recalculate it on save (or whenever it make sense). This > is often a good solution and it is better than complex queries in > terms of performance. There are some downsides to this method, though: It does take up some space in the database; it requires some processing every time an object is updated (and if your calculation involves any related objects, then you have to do it whenever *they* are updated, as well.) Additionally, if you ever change those rules, then you have to recalculate the value of that field for every instance in the database. That doesn't mean that it's a bad idea -- I use it all the time -- but you should at least think about it before doing it. An alternative, if you want to keep the code simple, and don't mind that it's less efficient than caching the field value, is to pull out the values from the database with the ORM, and then filter them in python: filter(lambda x: x.complete(), DinnerHost.objects.all()) or even like this: [x for x in DinnerHost.objects.all() if x.complete()] > > On 16 янв, 19:16, rmschne <rmsc...@gmail.com> wrote: >> Thanks. I'm still messing with this; but don't think it quite what I'm >> looking for. >> >> I want/need the Model Definition function complete() for use at the >> record level. I did this as a function since the rules are not based >> simply on the value of the fields. >> >> Ideally, I'd like to have the class DinnerHoseManager(modes.Manager) >> use the complete() function to help return the query set for where >> complete()=True. How to put that into this class? >> >> It's not the simple filter. Need a little more complexity to >> determining "complete" based on the two if statements. > > -- > 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. > > -- Regards, Ian Clelland <clell...@gmail.com> -- 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.