> Now, I want to select all records whose duration (end_time - > start_time) is more than 2 hours. > > I tried a custom method in the model like this which returns the > duration but cannot figure out how to use it in queryset filters.
Filtering by calculated fields is best done on the server side so you don't bring all the data across the pipe, just to throw it away. This may not matter if you only have a handful of records, but if your data collection will grow indefinitely, you certainly don't want to be pulling bajillions of records across the connection, just to skim through them and toss the uninteresting ones. If it's to be done on the DB-server side of things, the syntax is a bit dependant on the DB engine you're using. However, the basics: # sqlite would be something like items = items.extra(where=""" julianday(end_time) - julianday(start_time) > 2 * 60 * 60 * 100 """) # 60 minutes * 60 seconds * 100ths of a second # I don't know if the precision of julianday() varies by machine # postgresql's much cleaner syntax items = items.extra(where=""" start_time + interval '2 hours' > end_time """) Those are the two DBs I have at hand. If you're using a different DB back end, you'll have to look into its date-math syntax. It looks like the MySQL syntax might be something like items = items.extra(where=""" date_add(start_time, interval 2 hour) > end_time """) #close to PostgreSQL syntax, but with quotes and "hour" and on SQLServer, there's a DateDiff() function that one would want to use. I know it's a bear to deal with differing syntax...perhaps something like this might be a helpful wrapper function to include in the DB abstraction layer, much like the quote_name function, a "date-arithmetic" function that returns the DB-specific SQL clause for the difference between two dates/times/datetimes. Or perhaps there already is one, and I missed it? -tkc --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---