Connecting Models Together

Welcome back! After learning how to integrate SQLite3 with the Django ORM and create a simple model, it's time to extend those capabilities. In this unit, we'll focus on creating relationships between models. Just like in the real world, data often has connections and relationships that need to be represented within your database. By the end of this unit, you'll be comfortable defining these relationships in Django.

What You'll Learn

In this lesson, you’ll master the following:

  • Creating models that have relationships with each other
  • Connecting models using Django ORM
  • Creating views to handle data interactions involving related models

Firstly, let's consider a Category model that can be linked to multiple Todo items. This will allow each task to belong to a specific category. In your models.py, you define your models as follows:

Here, the Category model holds category names, and the Todo model has a foreign key connecting each task to a category. Foreign keys are used to establish relationships between models. In this case, the category field in the Todo model is a foreign key that links each task to a category.

Also, pay attention to the on_delete=models.CASCADE argument in the ForeignKey field. This argument specifies what happens when the linked category is deleted. In this case, it's set to CASCADE, which means that if a category is deleted, all tasks linked to that category will also be deleted.

Next, we’ll create views to handle adding new categories and tasks with their corresponding categories. Update your views.py file:

These views will enable you to add new categories and assign tasks to them through API requests.

Finally, don’t forget to map these views in your urls.py file:

This configuration sets up the URL routes to handle the creation of categories and tasks linked to them.

Now that we have both models and views in place, it’s time to test them out:

  1. First we need to create a category sending a a POST request to /add-category/ endpoint with the following JSON payload:

  2. Next, we can create a task linked to the category sending a POST request to /add-todo-with-category/ endpoint with the following JSON payload:

After sending these requests, you should see the new category and task added to the database.

Why It Matters

Understanding how to create and manage relationships between models is crucial for building robust and well-structured applications. With these skills, you will be able to:

  • Represent complex data relationships in your applications
  • Simplify data retrieval and manipulation using Django’s ORM
  • Build powerful and scalable web applications

By the end of this lesson, you will have a solid grasp of how to define and manage relationships between different parts of your data. This knowledge will form the backbone of efficient database management in your Django projects.

Are you excited to put this new knowledge into practice? Let's dive into the practice section and start coding!

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