Skip to content
Related Articles

Related Articles

Improve Article

Django Models | Set – 2

  • Last Updated : 21 Jun, 2019

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:

  • DecimalField have two required attributes.
    • max_digits
    • decimal_places
  • CharField has max_length as required attribute.
  • ForeignKey has on_delete as required attribute.

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.
admin-login
Create a super user as mentioned in this article.

Follow below mentioned steps to the create a superuser:

  • Stop the server using CTRL+C.
  • run command python manage.py createsuperuser
  • Enter your username which will be used for login
  • Enter your EmailId
  • Enter your password. To avoid any warning, make sure that password is not very common and entirely numeric.

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

Provide your credentials and log in.
django-admin-panel
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
no such table

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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :