试图从django中的基表筛选日期字段时出现名称错误



我有两种型号的

class Rule(models.Model):
pmdruleid = models.BigIntegerField(primary_key=True)
effectivedate = models.DateTimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'rule'
class Ruledefinitions(models.Model):
ruleactivestatus = models.CharField(max_length=14)
pmdclinicalruleid = models.OneToOneField(Rule, models.DO_NOTHING, db_column='pmdclinicalruleid', primary_key=True)
class Meta:
managed = False
db_table = 'ruledefinitions'
unique_together = (('pmdclinicalruleid', 'pmdclinicalvariableid'),)

我正在尝试根据视图集中的有效日期字段筛选记录,如下面的

class ActiveclientViewSet(viewsets.ModelViewSet):
queryset = Ruledefinitions.objects.select_related('pmdclinicalruleid').filter(pmdclinicalruleid__effectivedate < datetime.now())

我得到了NameError: name 'pmdclinicalruleid__effectivedate' is not defined

我改为

queryset = Ruledefinitions.objects.select_related('pmdclinicalruleid').filter(pmdclinicalruleid__effectivedate__lt = datetime.now())

它奏效了。问题是当使用lt时,没有必要使用>操作员

也许你可以在它周围加上单引号?

queryset = Ruledefinitions.objects.select_related('pmdclinicalruleid').filter('pmdclinicalruleid__effectivedate' < datetime.now())

相关内容

  • 没有找到相关文章

最新更新