djangodjango-modelsdropdownchoice

Select a valid choice. 2.6 is not one of the available choices


I have a model with a field having choices in it as given below. I am messing with the DecimalField somehow. And i cannot pass values like 1.7 or 2.6. But it is accepting 1.0 and 2.0 . Please let me know what am I doing wrong.

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)

Solution

  • You need to change field DecimalField to FloatField i give the new model with change

    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)
    

    see diff of DecimalField and FloatField

    Update this code to your code see it work. if it's work perfect or not let me know