Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Models Number Field

Tags:

django

models

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?

like image 687
Edward Knight Avatar asked Apr 09 '16 10:04

Edward Knight


People also ask

What is field in Django models?

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.

What is integer field?

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.

Can IntegerField be negative Django?

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).


1 Answers

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

like image 92
Khaled Al-Ansari Avatar answered Nov 16 '22 01:11

Khaled Al-Ansari