Himanshu Arora
Himanshu Arora

Reputation: 9

Ensure that there are no more than 0 digits before the decimal point

When I fill the bathroom field in my model with a value like 1.1, 2.5 anything with a digit before decimal it shows an error that no more than 0 digits before decimal point. If I give a value like .2, .3 then Django does not show any error. Same problem with the lot field.

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'

Upvotes: 1

Views: 1391

Answers (1)

Ken Y-N
Ken Y-N

Reputation: 15009

According to the manual:

For example, to store numbers up to 999 with a resolution of 2 decimal places, you’d use:

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

So, if you want up to 9.99 bathrooms, you would use:

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

(I'm not sure why you want a fractional number of toilets, but that's a separate issue...)

Upvotes: 2

Related Questions