Hi Jason, > Using the models below I'd like to be able to create Querysets such as: > > a = Treatment.objects.filter(total_dose__range=(4000,10000)) > b = a.filter(tumor__patient__gender = 'M') > > Both of the above work, but then I'd like to have an additional filter: > c = b.filter(tumor__patient__get_followup_length__gt = 5)
This is not possible because get_followup_length is not a field in the DB table but a model class method and Django doesn't support that (because it can't build a SQL statement out of this queryset definition.) Here are some alternatives: Option #1. Add a followup_length field to your Patient model. Rename the method get_followup_length to compute_followup_length and change it so that it stores its computed value into the new followup_length field. Then, call compute_followup_length() from delete() and save() methods in Tumor. In other words, everytime Tumor is changed, its Patient's followup_length gets computed. You can also achieve this via signals if you don't want to override Tumor.delete() and Tumor.save(). See: http://code.djangoproject.com/wiki/Signals Option #2. Filter the result set in memory using standard Python: c = [x for x in b if x.tumor.patient.get_followup_length() > 5] Note that, if you expect c to have a lot of rows, this will load them all in memory. And, obviously, c will be a list rather than a queryset. Hope this helps. -Rajesh --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---