field_name = models.PositiveSmallIntegerField ( ** options )
Illustration of PositiveSmallIntegerField using example. Consider a project named
pythonengineering that has an application named
Refer to the following articles to check how to create a project and an app in Django.
Enter the following code into the
models.py file of the geeks application.
| tr > |
Now when we run the
makemigrations command from the terminal,
Python manage. py makemigrations
# Generated by Django 2.2 .5 at 2019-09-25 06:00
migrations, models p>
Migration (migrations. Migration):
= c ode>
operations code >
, p >
Python manage.py migrate
geeks_field PositiveSmallIntegerField is created when you run migrations in the project. This is a field for storing positive integers.
PositiveSmallIntegerField is used to store an integer, represented in Python by an int instance. To learn more about Int, visit
# model import
# from the geek app
# create an instance
# create an instance
= code >
d) p >
Now let`s check it out on the admin server. We have created a GeeksModel instance.
Field parameters — they are arguments given to each field to apply some constraint or to convey a particular characteristic to a particular field. For example, adding the
null = True argument to PositiveSmallIntegerField will allow it to store empty values for that table in a relational database.
Here are the field options and attributes that PositiveSmallIntegerField can use.
|Null||If True , Django will store empty values as NULL in the database. Default is False.|
|Blank||If True , the field is allowed to be blank ... Default is False.|
|Choices||An iterable (eg, a list or tuple) of 2-tuples to use as choices for this field.|
|Default||The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.|
|help_text||Extra “help” text to be displayed with the form widget. It`s useful for documentation even if your field isn`t used on a form.|
|primary_key||If True, this field is the primary key for the model .|
|Unique||If True, this field must be unique throughout the table.|
A Practical Approach to Computer Algorithms Using Python® and C# Rod Stephens started out as a mathematician, but while studying at MIT, he discovered how much fun algorithms are. He took every al...
Spark is one of the hottest technologies in big data analysis right now, and with good reason. If you work for, or you hope to work for, a company that has massive amounts of data to analyze, Spark of...
Taking into account the development of modern programming, especially the emerging programming languages that reflect modern practice, Numerical Programming: A Practical Guide for Scientists and...
A Practical, No-Nonsense Introduction to Python Development. You already know you want to learn Python, and a smarter way to learn Python 3 is to learn by doing. The Python Workshop focuses on buil...