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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.