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)
I'm not sure how to implement this. Any help would be appreciated.
class Patient(models.Model):
class Admin:
fields = (
(None,
{'fields':('name','gender','age','followup_date','expiration_date','flag')}),
)
GENDER_CHOICES = (('M','Male'), ('F','Female'))
name = models.CharField(max_length = 100)
gender = models.CharField(max_length=1,choices=GENDER_CHOICES)
age = models.IntegerField()
expiration_date = models.DateField(blank=True,null=True)
flag = models.IntegerField(blank=True,null=True)
followup_date = models.DateField(blank=True,null=True)
def get_followup_length(self):
if self.followup_date is None:
return None
comp_dates = [t.get_latest_completion_date() for t in
self.tumor_set.all()]
if comp_dates:
lcd = max(comp_dates)
follow_up_length = self.followup_date - lcd
months = follow_up_length.days / 30.42
return int(months)
else:
return None
def __unicode__(self):
return self.name
class Tumor(models.Model):
class Admin:
pass
patient = models.ForeignKey(Patient)
stage = models.CharField(max_length =
100,blank=True,null=True,core=True)
pathology = models.CharField(max_length =
100,blank=True,null=True,core=True)
description = models.CharField(max_length =
200,blank=True,null=True,core=True)
def get_latest_completion_date(self):
return self.treatment_set.all
().latest('completion_date').completion_date
def __unicode__(self):
str = "%d - %s" % (self.id,self.patient.name)
return str
class Treatment(models.Model):
class Admin:
pass
tumor = models.ForeignKey(Tumor,edit_inline=models.TABULAR
,num_in_admin=3)
start_date = models.DateField(core=True)
completion_date = models.DateField(core=True)
fractions = models.IntegerField(core=True)
dose_per_frac = models.IntegerField(core=True)
total_dose = models.IntegerField(core=True)
def __unicode__(self):
str = "%s - %s to %s" % (self.tumor.patient.name,self.start_date,
self.completion_date)
return str
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---