Introduction

Welcome to the first lesson of our course on "Advanced Database Schema Design in Django." In this lesson, we will introduce you to the concept of one-to-one relationships in databases. Understanding how to create and manage these relationships is essential for building robust and efficient applications.

By the end of this lesson, you will know how to:

  1. Create a Note model.
  2. Link the Note model to an existing Todo model using a one-to-one relationship.
  3. Create serializers for both models.
  4. Use fixtures to pre-load data.
  5. Write tests to ensure everything works correctly.

Let's get started!

Recap: Project Setup

Before we dive into creating new models, let's quickly recap our existing setup. In previous lessons, you learned how to create a Todo model. Here is what the model looks like:

from django.db import models

class Todo(models.Model):
    task = models.CharField(max_length=255)
    completed = models.BooleanField(default=False)
    priority = models.IntegerField()
    assignee = models.CharField(max_length=255, blank=True, null=True)
    group = models.CharField(max_length=255, blank=True, null=True)

This model represents tasks that need to be done, with fields for the task name, completion status, priority, assignee, and group information.

Creating the Note Model

Now, let's create a Note model to represent notes that can be linked to Todo tasks.

Here's how you can create the Note model:

from django.db import models

class Note(models.Model):
    content = models.TextField()
    
    def __str__(self):
        return self.content[:50]
  • content: A TextField to store the note's text content.
  • __str__ method: Returns the first 50 characters of the note's content for easy identification.
Establishing the One-to-One Relationship

Now, imagine that in your app, one note item always corresponds to exactly one todo item. For example, notes can be additional details added to a Todo item card. Why don't we make a note of a simple CharField inside the Todo model? Well, there could be several reasons not to do this. The most simple one is that the note could be created by another user, and the creator of a card shouldn't have access to edit or remove a note, while the note's creator shouldn't have access to modify todo's fields. In this case, to control the access rights, it would be the best option to create a separate Note model and link it to the Todo model.

To link a Note model with a Todo model using a one-to-one relationship, we will add a OneToOneField to the Todo model.

Here is the updated Todo model:

from django.db import models

class Todo(models.Model):
    task = models.CharField(max_length=255)
    completed = models.BooleanField(default=False)
    priority = models.IntegerField()
    assignee = models.CharField(max_length=255, blank=True, null=True)
    group = models.CharField(max_length=255, blank=True, null=True)
    note = models.OneToOneField(Note, on_delete=models.SET_NULL, null=True, blank=True)

    def __str__(self):
        return self.task
  • note is a OneToOneField that links each Todo instance to a Note instance.
  • on_delete=models.SET_NULL: If the Note instance is deleted, set the note field in the Todo model to None.
  • null=True, blank=True: Allows the note field to be optional.
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal