django 确保小数点前不超过0位

np8igboo  于 2023-06-25  发布在  Go
关注(0)|答案(2)|浏览(104)

当我在模型中的bathroom字段中填入1.1,2.5这样的值时,任何小数点前有一位的值都会显示错误小数点前不超过0位。如果我给予一个像**.2,.3**这样的值,那么Django不会显示任何错误。lot字段也有同样的问题。

class listing(models.Model):
    realtor = models.ForeignKey(Realtor,on_delete=models.DO_NOTHING)
    title = models.CharField(max_length=200)
    address = models.CharField(max_length=200)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100)
    state = models.CharField(max_length=20)
    description = models.TextField(blank=True)
    price = models.IntegerField()
    bedrooms = models.IntegerField()
    bathrooms = models.DecimalField(max_digits=2, decimal_places=2)
    garage = models.IntegerField(default=0)
    sqft = models.IntegerField()
    lot_size = models.DecimalField(max_digits=2, decimal_places=2)
    photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/')
    photo_1 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank = True)
    photo_2 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank = True)
    photo_3 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank = True)
    photo_4 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank = True)
    photo_5 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank = True)
    photo_6 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank = True)
    is_published = models.BooleanField(default= True)
    list_date = models.DateTimeField(default=datetime.now, blank=True)
    def __str__(self):
        return f'self.date'
xlpyo6sf

xlpyo6sf1#

根据手册:
例如,要存储高达999的数字,分辨率为2个小数位,您可以用途:

  • models.DecimalField(...,max_digits=5,decimal_places=2)

所以,如果你想要多达9.99个浴室,你可以用途:

bathrooms = models.DecimalField(max_digits=3, decimal_places=2)

(我不知道为什么你想要一个分数的厕所,但这是一个单独的问题...)

tzxcd3kk

tzxcd3kk2#

max_digits和decimal_places参数是否必须与输入的数字值完全相同?根据上面的例子,我只能输入0.00 - 9.99之间的值吗?如果是,我如何解释动态生成的十进制值?

相关问题