ORM

  • 1. What is ORM ?
    ORM : Object Relation Mapping is the programming technique to map application model objects to the relational database tables.

    2. Mapping Example

    The ORM maps database tables to Python classes and database records to instances of those classes,.

    Table_X in the database represents Class_X.

    Object_X1 and Object_X2 are instances of Class_X.

    Individual rows in Table_X represent Object_X1 and Object_X2, as well as any other instances of Class_X.

    ORM also maps objects attributes to respective table fields.

  • 2. use of ORM ?

    ORM allows developers to interact with a relational database using Python objects instead of writing SQL queries directly. It also allows developers to perform common database operations such as creating, reading, updating, and deleting records using Python methods and attributes.

  • 3. How does Django ORM work?

    1. Python application gives the query wrritten in python to ORM

    2. Then ORM converts this query into an abstract representation and gives to the adaptor or driver

    3. The adaptor or driver converts the Python query into a raw SQL query and then excute the query, and then return the result to the ORM

  • 4. Setting Up Django ORM

    1. create django project

    2. Open your project’s settings.py file and Locate the DATABASES configuration block.

    3. update database engine, db name, username and password

    Defining Models

    1. define the model in models.py

    2. Django ORM automatically create the corresponding table in the database