我是 django 和 python 的新手。我正在尝试找出进行查询的最佳(最高性能)方法。
这是我的模型:
class Immunization(models.Model):
name = models.CharField(max_length=12, primary_key=True)
verbose_name = models.CharField(max_length=80)
desc = models.CharField(max_length=800)
effective_duration = TimedeltaField()
def __unicode__(self):
return self.name
class Patient(models.Model):
name = models.CharField(max_length=64)
age = models.IntegerField()
birthday = models.DateField()
def __unicode__(self):
return self.name
'''
ImmunizationRecord is a specific date an immunization was administered to a given patient.
'''
class ImmunizationRecord(models.Model):
patient = models.ForeignKey('Patient')
immunization = models.ForeignKey('Immunization')
date_administered = models.DateTimeField(auto_now_add=True)
我要执行的查询是:
*获取患者在过去 Immunizations.effective_duration 中未接受的所有免疫接种。*
到目前为止,我正在做这样的事情:
def get_context_data(self, **kwargs):
context = super(ProfileView, self).get_context_data(**kwargs)
all = Immunization.objects.all()
done = ImmunizationRecord.objects.filter(patient__name=self.request.user)
for r in done:
#TODO: add date check for expiry
all = [s for s in all if r.immunization.name != s.name]
context['available_list'] = all
return context