[Answer]-Adding number and Symbols in Django

1👍

You can use validators in your model and add the percentage in your template when you are displaying the numbers. This allows you to do any manipulations easily.

from django.core.validators import MaxValueValidator, MinValueValidator
    #in your model..
    percentage = models.FloatField(
        validators = [
            MinValueValidator(0.0), MaxValueValidator(100.0)
    ])
👤pad

0👍

If you want only float numbers with percent symbol saved in database, this regex ^\d+.\d+%+$ will do the job.
NOTE: by default validation on fields will work only with usage in ModelForm.
If you want to check it every time, override save in your class in this way:

    def save(self, force_insert=False, force_update=False, using=None,
         update_fields=None):
    self.full_clean()
    return super(YourModel, self).save(force_insert=force_insert, force_update=force_update,
                                   using=using, update_fields=update_fields)

Leave a comment