Django Series for beginners

Getting Started with Django Models (ORM): A Beginner’s Guide

Spread the love

Django is a popular web framework for building web applications with Python. One of the key features of Django is its use of models to represent the data in your application. In this article, we’ll explore how to use Django models to manage the data in your application.

To get started, let’s first understand what Django models are and how they work.

What are Django Models?

In Django, a model is a class that represents a table in your database. Each model maps to a single database table, and each model field maps to a column in that table.

For example, if you have a model called Person, it might have fields like first_name, last_name, and age. These fields would correspond to columns in the person table in your database.

Models are used to store and retrieve data from the database. They provide a high-level interface for interacting with the data in your application, allowing you to create, read, update, and delete (CRUD) records in the database without having to write raw SQL queries.

Defining a Django Model

To define a Django model, you’ll need to create a Python class that subclasses django.db.models.Model. Each field in the model is represented as a class attribute, and each attribute is an instance of the appropriate field type.

Here’s an example of a simple Django model:

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    age = models.PositiveIntegerField()

In this example, we’ve defined a Person model with three fields: first_name, last_name, and age. The CharField field type is used for character data, such as first and last names, and the PositiveIntegerField field type is used for the age field.

There are many different field types available in Django, including CharField, TextField, IntegerField, BooleanField, and more. You can find a full list of field types in the Django documentation.

Once you’ve defined your model, you’ll need to run a migration to create the corresponding table in the database. To do this, you’ll use the makemigrations and migrate commands provided by Django’s manage.py script.

python manage.py makemigrations
python manage.py migrate
 

Working with Django Models

Now that you’ve defined your model and created the corresponding table in the database, you can start using it to manage your data. Django provides a high-level interface for interacting with models, allowing you to perform CRUD operations without having to write raw SQL queries.

Here are some examples of common operations you might perform with Django models:

Creating Records

To create a new record in the database, you can use the create method of your model.

person = Person.objects.create(first_name='John', last_name='Doe', age=30)
This will create a new Person record in the database with the given first name, last name, and age.
 

Reading Records

To retrieve records from the database, you can use the objects manager provided by Django. The objects manager.

The objects manager provides several methods for querying the database, such as all, filter, and get.

For example, to retrieve all Person records from the database, you can use the all method:

people = Person.objects.all()

To filter the records based on certain criteria, you can use the filter method:

 adults = Person.objects.filter(age__gt=18)

This will retrieve all Person records where the age field is greater than 18.

You can also use the get method to retrieve a single record based on its primary key:

person = Person.objects.get(pk=1)

Updating Records

To update a record in the database, you can retrieve it using one of the methods mentioned above, modify its field values, and then call the save method:

person = Person.objects.get(pk=1)
person.first_name = 'Jane'
person.save()

This will update the first_name field of the Person record with a primary key of 1 to ‘Jane’.

Deleting Records

To delete a record from the database, you can use the delete method:

person = Person.objects.get(pk=1)
person.delete()

This will delete the Person record with a primary key of 1 from the database.

Conclusion

In this article, we’ve covered the basics of using Django models to manage the data in your application. Django models provide a high-level interface for interacting with the database, allowing you to perform CRUD operations without having to write raw SQL queries. With a little practice, you’ll be able to easily use Django models to manage the data in your application.

Leave a Comment

Your email address will not be published. Required fields are marked *