Register to get access to free programming courses with interactive exercises

ORM Key Aspects of Web Development in Python

Programmers develop each piece of software for a specific domain, for example:

  • An analytics system might work with view, session, funnel, and cohort
  • The concepts will be different in an online store — product, category, and payment gateway

Together they form the domain ontology. You can find the term category model in some sources. It is the same as ontology. In addition to the concepts themselves, ontologies include descriptions of their relationships. For example, the User entity relates to the Purchase entity in a one-to-many relationship. It means users can make any number of purchases, but each purchase belongs to only one user.

The domain model is the basis for communication and understanding between team members. It doesn't depend on the programming language or programming in general. It doesn't matter who is communicating — programmers with each other or programmers with customers, managers, or designers, it's all the same. You work with the domain's entities and relationships and the business rules used in that program.

Such rules may include automatically including a discount when ordering a certain amount of goods:

ERD

It's important to understand that the model reflects only a part of the domain in some detail. And the model can vary in programs written by different developers, even if they're from the same field.

Some domains have a set of fixed entities, relationships, and rules of operation. We can see it in accounting, for example. But there are also less formal areas where there are even more possibilities. Here are some examples from Hexlet. The number of entities is more than a hundred, the number of links is several hundred, and the number of rules is difficult to count. There are so many of them too.

We create a data model in code based on the domain model. We construct entities and define their relationships. Then we build a working code that operates on the entities based on requirements and business rules. You may be wondering how to map these entities to the database where they're stored.

The easiest way is to create a table for each entity and link them with foreign keys. Most projects do this. We can do it by the Object Relational Mapper (ORM) — a data framework.

The ORM helps describe entities and their relationships and determines how the entity is mapped to the database (usually semi-automatically). The ORM takes care of a part of the work:

  • Generating SQL queries
  • Extracting data
  • Casting - converting database types into target language types and vice versa
  • Automatic link extraction

The result is that the ORM hides all the work with the database. All you need is the configuration from the programmer, and the ORM itself can perform all the necessary queries. You still have to write your own in complex cases, but it's not that difficult. The ORM includes a query builder that simplifies the generation of SQL.

There are several ORMs in the Python ecosystem. It includes independent ORMs and ORMs designed for specific frameworks and shipped with those frameworks. Let us look at an example implemented using Django ORM. The definition of the Photo entity looks like this:

from django.db import models

class Photo(models.Model):
    title = models.CharField(max_length=200)
    image = models.ImageField()
    slug = models.SlugField()

Usage:

# retrieve objects from the database
photos = Photo.objects.all()
# pass them to the template
render(request, 'polls/detail.html', {'photos': photos})

The code that describes the entity may seem simple, but the level of automation in Django ORM is very high. Under the hood of this simple code is the entity creation in the database and a set of checks on the values you put into the model. Before you start working with an ORM, you need to learn the basics of databases by working directly with the database.

It is also essential to become familiar with it:

  • Normalization
  • Foreign and primary keys
  • Indexes
  • Request plans
  • The SQL language, which helps you change database structures and manipulate data within the database

Then you can go to the level of executing queries from the programming language. Python uses several libraries like postgres for this. Here are just some of the topics involved in the code above:

  • Entity-relation model
  • Domain-driven design
  • ActiveRecord / DataMapper
  • Identity map
  • Migration
  • Validation
  • Metaprogramming

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY
Suggested learning programs
profession
new
Developing web applications with Django
10 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.