类属性查询法

class RiskLevel_Months(models.Model):
    """
    每月风险等级表 --nzy
    """
    levels = (('0', '无风险'), ('1', '中等风险'), ('2', '高风险'))
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name=u'编号', help_text=u'编号')
    # month = models.CharField(max_length=50, choices=months, verbose_name=u'月份', help_text=u'月份', null=True, blank=True)
    # level = models.CharField(max_length=50, choices=levels, verbose_name=u'等级', help_text=u'等级', default='0')
    biological_risk = models.OneToOneField(BiologicalRisk, verbose_name="海洋生物风险ID", help_text="海洋生物风险ID",
                                           on_delete=models.CASCADE, related_name="biological_risk_month")
    Jan = models.CharField(max_length=8, choices=levels, verbose_name=u'一月', help_text=u'一月', default='0')
    Feb = models.CharField(max_length=8, choices=levels, verbose_name=u'二月', help_text=u'二月', default='0')
    Mar = models.CharField(max_length=8, choices=levels, verbose_name=u'三月', help_text=u'三月', default='0')
    Apr = models.CharField(max_length=8, choices=levels, verbose_name=u'四月', help_text=u'四月', default='0')
    May = models.CharField(max_length=8, choices=levels, verbose_name=u'五月', help_text=u'五月', default='0')
    Jun = models.CharField(max_length=8, choices=levels, verbose_name=u'六月', help_text=u'六月', default='0')
    Jul = models.CharField(max_length=8, choices=levels, verbose_name=u'七月', help_text=u'七月', default='0')
    Aug = models.CharField(max_length=8, choices=levels, verbose_name=u'八月', help_text=u'八月', default='0')
    Sept = models.CharField(max_length=8, choices=levels, verbose_name=u'九月', help_text=u'九月', default='0')
    Oct = models.CharField(max_length=8, choices=levels, verbose_name=u'十月', help_text=u'十月', default='0')
    Nov = models.CharField(max_length=8, choices=levels, verbose_name=u'十一月', help_text=u'十一月', default='0')
    Dec = models.CharField(max_length=8, choices=levels, verbose_name=u'十二月', help_text=u'十二月', default='0')

    @classmethod
    def high_risk_biolog(cls):  #获取所有数据中当前月份的高风险生物
        return cls.objects.filter(**{datetime.now().strftime('%b'): cls.levels[2][0]}).values('biological_risk')

    class Meta:
        db_table = 'envidata_risklevel_months'
        verbose_name = "每月风险等级表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.id)

你可能感兴趣的:(类属性查询法)