DEV Community

Cover image for Working with Databases in Django Using PostgreSQL
kihuni
kihuni

Posted on

Working with Databases in Django Using PostgreSQL

Introduction

Django simplifies database interaction with its Object-Relational Mapping (ORM) system, enabling Python code to be used for working with databases instead of writing SQL. This guide provides essential information on using PostgreSQL in Django, from database setup to performing CRUD operations.

Setting Up the Database

Database Configuration

  • Installing PostgreSQL: Ensure that PostgreSQL is installed on your system. If you're new to Postgres, here's a guide to help you get started: Getting Started with PostgreSQL.
  • Installing psycopg2: Install the PostgreSQL adapter for Python.
pip install psycopg2-binary
Enter fullscreen mode Exit fullscreen mode
  • Configure your database settings in the settings.py file.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
Enter fullscreen mode Exit fullscreen mode

Defining Models

Creating a Model

A model in Django is a Python class that subclasses django.db.models.Model and defines fields and behaviors of the data you're storing.

For example:

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)
    birth_date = models.DateField()
Enter fullscreen mode Exit fullscreen mode

Fields

Field Types: Django provides various field types to map Python types to PostgreSQL columns.

  • CharField: Stores text data with a maximum length.
  • IntegerField: Stores integer values.
  • DateField: Stores dates.
  • ForeignKey: Defines a many-to-one relationship.

Meta Class

The Meta class inside a model contains metadata options.

Example:

class Author(models.Model):
    name = models.CharField(max_length=100)
    birth_date = models.DateField()

    class Meta:
        ordering = ['name']
Enter fullscreen mode Exit fullscreen mode
  • ordering: Orders query results by the specified fields.

Migrating the Database

  • Create Migrations: Generate migration files based on your models.
python manage.py makemigrations
Enter fullscreen mode Exit fullscreen mode
  • Apply Migrations: Apply the migration files to update your database schema.
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

CRUD Operations

Creating Records

  • Using the ORM: Create new records by instantiating a model and calling the save() method.
# python manage.py shell

from postgress_database.models import Author 

author = Author(name='John Doe', birth_date='1980-01-01')
author.save()

Enter fullscreen mode Exit fullscreen mode

Reading Records

QuerySet API: Retrieve records using methods like all(), filter(), get(), etc.

Example:

authors = Author.objects.all()
author = Author.objects.get(id=1)
filtered_authors = Author.objects.filter(name__icontains='john')

Enter fullscreen mode Exit fullscreen mode

Updating Records

  • Modifying and Saving: Update records by modifying model instances and calling save().
author = Author.objects.get(id=1)
author.name = 'Jane Doe'
author.save()

Enter fullscreen mode Exit fullscreen mode

Deleting Records

Delete Method: Remove records using the delete() method.

author = Author.objects.get(id=1)
author.delete()

Enter fullscreen mode Exit fullscreen mode

QuerySet Methods

Common Methods

  • all(): Returns all records.
authors = Author.objects.all()

Enter fullscreen mode Exit fullscreen mode
  • filter(): Returns records matching the given criteria.
filtered_authors = Author.objects.filter(name__icontains='john')

Enter fullscreen mode Exit fullscreen mode
  • exclude(): Excludes records matching the given criteria.
non_john_authors = Author.objects.exclude(name__icontains='john')

Enter fullscreen mode Exit fullscreen mode
  • order_by(): Orders records by the specified field.
ordered_authors = Author.objects.order_by('name')

Enter fullscreen mode Exit fullscreen mode
  • values(): Returns a QuerySet of dictionaries instead of model instances.
author_values = Author.objects.values('name', 'birth_date')

Enter fullscreen mode Exit fullscreen mode

Relationships

One-to-Many Relationships

  • ForeignKey: Define a many-to-one relationship using ForeignKey
class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

Enter fullscreen mode Exit fullscreen mode

Many-to-Many Relationships

class Publisher(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=200)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)

Enter fullscreen mode Exit fullscreen mode

One-to-One Relationships

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField()

Enter fullscreen mode Exit fullscreen mode

Conclusion

Working with databases in Django involves understanding how to configure your database, define models, and perform CRUD operations efficiently. Using PostgreSQL with Django's ORM simplifies these tasks, allowing you to write Python code instead of SQL queries. This guide provides a foundational understanding.

Top comments (0)