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