Quick Contact

    Python Tutorial
    Python Panda Tutorial
    Python Selenium
    Python Flask Tutorial
    Python Django
    Numpy
    Tensorflow
    Interview Questions & Answers

    Django Models

    Introduction to Django Models

    A Model in Django is a class imported from the django.db library that acts as the bridge between your database and server. This class is a representation of the data structure used by your website. It will directly relate this data-structure with the database. So that you don’t have to learn SQL for the database.

    Also, every database implements SQL in a different way, because they all are made to complete a different task.

    When you create a model, Django executes SQL to design a corresponding table in the database (as shown below) without the need to write even a single line of SQL. Django prefixes the name of the table with your Django application name. Also, the model links related information in the database.

    Creating Your First Django Model

    Let’s do it step by step, also we are assuming that you have a database connected to your server or project otherwise nothing written here will work.

    We are going to create a student model where we will have fields like roll_no., name, class and department.

    1. Firstly, create a new app by this command:
    2. django-adminstartapp student

    3. After creating the application you must install it in your project. To do that add ‘student’ application to INSTALLED_APPS List in settings.py file.
    4. INSTALLED_APPS = [
      ‘django.contrib.admin’,
      ‘django.contrib.auth’,
      ‘django.contrib.contenttypes’,
      ‘django.contrib.sessions’,
      ‘django.contrib.messages’,
      ‘django.contrib.staticfiles’,
      ‘student’,
      ]
      

      Then save your changes.

    5. Open the models.py file in that and write this code in it
    6. fromdjango.db import models
      
      class Student(models.Model):
      roll_no = models.TextField()
      name = models.TextField(max_length = 40)
      stud_class = models.TextField()
      department = models.TextField()
      

      Here you can see that the models class has lots of methods for fields corresponding to web input. We are using text fields here as they are easier to implement and takes in any input.

    7. Now, execute these commands.
    8. python manage.py makemigrations

      python manage.py migrate

      Make sure that you have the database connected and you have started the Apache server and MySQL server in the database.

    9. Open the localhost/phpmyadmin/
    10. Now open the main database that you have created for this Django project. Then, check the table student_student
    11. You will see these fields.

      Advantages of Django Models

      A model as stated in the definition is the link between the server and your database. Now, whenever you need the data or any operation is performed where data from the server is needed which is essentially just retrieving data from your database, it will need some middleware or bridge which can convert that data in a transmittable/Http response or more generally a web-transmittable format. There, Model comes in and does this important work for you.

      If you don’t use the Django models, then you will have to perform a lot of work manually, like writing code that can connect your database with the Django server. In this case, you have to be an expert programmer. So our recommendation is that you shouldn’t bother yourself whether you master any database or not.

      Model not only retrieves the data and converts it into the desirable format but execute it by applying business logic, or the logical part/ backend of your website that is actually inside the model component.

      Not every operation is executable in the database and therefore we need backend which can actually perform some operation on our retrieved data, in this case, that language is Python.


    Apply now for Advanced Python Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.