1

我有一个模型,其中有一个字段可供选择,如下所示。我以某种方式弄乱了 DecimalField。而且我不能传递像 1.7 或 2.6 这样的值。但它接受 1.0 和 2.0 。请让我知道我做错了什么。

class Trip(models.Model):

    ACE = 1.00
    Dosth = 1.70
    TEN = 2.00
    FORTEEN = 2.30
    SEVENTEEN = 2.60
    NINETEEN = 2.90
    TWENTY = 3.10
    TWENTYTWO = 3.10

    VEH_CHOICES = (
        (ACE, 'ACE'),
        (Dosth, 'Dosth'),
        (TEN, '10.5FT'),
        (FORTEEN, '14FT'),
        (SEVENTEEN, '17FT'),
        (NINETEEN, '19FT'),
        (TWENTY, '20FT'),
        (TWENTYTWO, '22FT'),
    )
    ftype = models.DecimalField(null = True, blank = True, verbose_name = "Vehicle Type", decimal_places = 2, max_digits = 5, choices = VEH_CHOICES)
4

1 回答 1

1

您需要更改字段DecimalField以更改FloatField新模型

class Trip(models.Model):

    ACE = 1.00
    Dosth = 1.7
    TEN = 2.00
    FORTEEN = 2.30
    SEVENTEEN = 2.60
    NINETEEN = 2.90
    TWENTY = 3.10
    TWENTYTWO = 3.10

    VEH_CHOICES = (
        (ACE, 'ACE'),
        (Dosth, 'Dosth'),
        (TEN, '10.5FT'),
        (FORTEEN, '14FT'),
        (SEVENTEEN, '17FT'),
        (NINETEEN, '19FT'),
        (TWENTY, '20FT'),
        (TWENTYTWO, '22FT'),
    )
    ftype = models.FloatField(null = True, blank = True, verbose_name = "Vehicle Type", choices = VEH_CHOICES)

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

DecimalField查看和的差异FloatField

将此代码更新为您的代码,看看它是否有效。如果它工作完美或不让我知道

于 2020-03-06T12:18:34.833 回答