Create Models in Django
Models in django
A model is a class that represents a table in our database. Every attribute of the class is a field of the table. Models are defined in the directory : appname/models.py. In our example : accounts/models.py.
Open the models.py and create a class with the name of the table you want to create in the database and add the fields in that class as shown below:
Now run the following command to create the migration:
You will get the migrations in the appname/migrations folders.
Open the migration file and you will be able to see all the fields you had created in the models.
Now you have to register the model in the admin.py.
Open the admin.py file and add the below code:
Now migrate the table using the following command :
Now you can check your database. The table and the table structure will be visible there.
You can see the newly created table in the admin panel under the appname heading(accounts) by going to the below given url in your browser:
Tags: Funda of web it Django tutorials, Django tutorials, Django tutorials funda, django for beginners, how to make migrations in django, django models, how to make models in django, make models and migrations in django, how to make migrations and models in django
Latest PostsREST API using Django Rest Framework How to connect Django project with MySQL database How to delete image from folder when deleting a record in django How to update new image in django How to fetch / retrieve images in django How to upload image in django Deploy Django App on Heroku for free Upload Files/Images in Django How to show success message in django Delete data from database in Django
© Copyright - 2018 - 2022 | All rights reserved at Funda of Web IT