Wednesday, May 6, 2020

Real Python: How to Move a Django Model to Another App

If you’ve ever thought about refactoring your Django app, then you might have found yourself needing to move a Django model around. There are several ways to move a Django model from one app to another using Django migrations, but unfortunately none of them are straightforward.

Moving models between Django apps is usually a very complicated task that involves copying data, changing constraints, and renaming objects. Because of these complications, the Django object-relational mapper (ORM) does not provide built-in migration operations that can detect and automate the entire process. Instead, the ORM provides a set of low-level migration operations that allow Django developers to implement the process themselves in the migration framework.

In this tutorial, you’ll learn:

  • How to move a Django model from one app to another
  • How to use advanced features of the Django migration command line interface (CLI), such as sqlmigrate, showmigrations, and sqlsequencereset
  • How to produce and inspect a migration plan
  • How to make a migration reversible and how to reverse migrations
  • What introspection is and how Django uses it in migrations

After completing this tutorial, you’ll be able to choose the best approach for moving a Django model from one app to another based on your specific use case.

Free Bonus: Click here to get access to a free Django Learning Resources Guide (PDF) that shows you tips and tricks as well as common pitfalls to avoid when building Python + Django web applications.

Example Case: Move a Django Model to Another App

Throughout this tutorial, you’re going work on a store app. Your store is going to start with two Django apps:

  1. catalog: This app is for storing data on products and product categories.
  2. sale: This app is for recording and tracking product sales.

After you finish setting up these two apps, you’re going to move a Django model called Product to a new app called product. In the process, you’ll face the following challenges:

  • The model being moved has foreign key relationships with other models.
  • Other models have foreign key relationships with the model being moved.
  • The model being moved has an index on one of the fields (besides the primary key).

These challenges are inspired by real-life refactoring processes. After you overcome them, you’ll be ready to plan a similar migration process for your specific use case.

Setup: Prepare Your Environment

Before you start moving things around, you need to set up the initial state of your project. This tutorial uses Django 3 running on Python 3.8, but you can use similar techniques in other versions.

Set Up a Python Virtual Environment

First, create your virtual environment in a new directory:

$ mkdir django-move-model-experiment
$ cd django-move-model-experiment
$ python -m venv venv

For step-by-step instructions on creating a virtual environment, check out Python Virtual Environments: A Primer.

Create a Django Project

In your terminal, activate the virtual environment and install Django:

$ source venv/bin/activate
$ pip install django
Collecting django
Collecting pytz (from django)
Collecting asgiref~=3.2 (from django)
Collecting sqlparse>=0.2.2 (from django)
Installing collected packages: pytz, asgiref, sqlparse, django
Successfully installed asgiref-3.2.3 django-3.0.4 pytz-2019.3 sqlparse-0.3.1

You’re now ready to create your Django project. Use django-admin startproject to create a project called django-move-model-experiment:

$ django-admin startproject django-move-model-experiment
$ cd django-move-model-experiment

After running this command, you’ll see that Django created new files and directories. For more about how to start a new Django project, check out Starting a Django Project.

Create Django Apps

Read the full article at https://realpython.com/move-django-model/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...