The Wayback Machine - https://web.archive.org/web/20240118165832/https://www.geeksforgeeks.org/django-models-set-2/
Open In App
Related Articles

Django Models | Set – 2

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 21 Jun, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials