如何对Django FloatField / DecimalField执行比较操作

时间:2018-10-02 18:01:25

标签: django django-models django-views

这是我的模特。

class Student(models.Model):
    email = models.EmailField(unique=True)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, null=True)
    usn = models.CharField(max_length=20, null=False, unique=True)
    full_name = models.CharField(max_length=256, null=False)
    contact_no = models.CharField(max_length=10, null=False)
    tenth = models.FloatField(default=0)
    twelfth = models.FloatField(default=0)
    cgpa = models.FloatField(default=0)
    backlog = models.IntegerField(null=True)
    active_backlog = models.IntegerField(default=0)
    branch = models.ForeignKey(Branches, on_delete=models.SET_NULL, null=True)
    pc = models.ForeignKey(PC, on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return self.full_name

我无法过滤以下查询。

Student.objects.filter(tenth__gt='some decimal value')

示例

Student.objects.filter(tenth__gt=10.0)

它会引发错误

decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

它适用于IntegerField,但不适用于FloatFieldDecimalField

1 个答案:

答案 0 :(得分:0)

尝试:

Student.objects.filter(tenth=some decimal value without quotes)