Open In App

Django Models | Set – 2

Model Fields –

Model fields define the datatype which will be stored in the provided variable. To store price, the integer type is more suitable. To store height, length etc. float/decimal type is more suitable. To store title or heading, it is better to provide character limit. For writing an article, it is better to have a textbox with no character limit.

Example:




Price = models.IntegerField()
Screen_size = models.DecimalField(max_digits = 2, decimal_places = 2)
Color = models.CharField(max_length = 120)
Description = models.TextField()

For some fields, some of the parameters are required. Some of them are listed below which are most frequently used:



A complete reference to model fields and their details can be found on Django official website.

Admin Interface –

After you define attribute names and their type in your class defined in /models.py, start your server using following command:



python manage.py runserver

Now, open admin panel in localhost address:

127.0.0.1:8000/admin

You will be prompted to login page.

Create a super user as mentioned in this article.

Follow below mentioned steps to the create a superuser:


Now, run your server again and goto 127.0.0.1:8000/admin.

Provide your credentials and log in.

Click on users and you will see all users and details. Right now, we are having only one user which is our admin.

Now click on Phones and you will get an error page displayed below

OperationalError at /admin/product/phone/
no such table: product_phone

Traceback helps you to debug your code easily but debug mode should be turned off during deployment otherwise any user can exploit your website’s critical information.

Article Tags :