In Django I'm trying to create a model which will contain a field for player's shirt numbers and was wondering if there is a way to restrict the field to be only numerical inputs. Currently I have in the models.py
number = models.CharField(max_length=2)
Is there a different field to CharField that will allow me to restrict the input to only numbers?
Fields in Django are the data types to store a particular type of data. For example, to store an integer, IntegerField would be used. These fields have in-built validation for a particular data type, that is you can not store “abc” in an IntegerField. Similarly, for other fields.
In Emptoris® Sourcing, integer fields are fields in which you can make number entries and if required also use the values for calculations. Other than the basic information required for creating this field type, the table following table lists some additional fields.
The IntegerField is used to store integer values from -2147483648 to 2147483647 ( 4 Bytes ). default parameter is not mandatory. But it's useful to set a default value. Like an IntegerField, but must be either positive or zero (0).
One of the cool things about Django that it provides you with different types of models read about them here.
For your case try something like this
number = models.IntegerField()
You can read more about the IntegerField
here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With