If you specify a foreign key in Django's model change, you need to specify on_delete.

I was worried about on_delete when I tried to set a foreign key in Django, so I summarized it.

What i was doing

I'm currently building a web app to hold game tournaments, so I needed a table to connect the tournament with the participants.

So I created a model in which the participant table has a tournament table and a user table with foreign keys.

models.py


from django.db import models
from django.contrib.auth.models import User
from games.models import Game


class Participations(models.Model):
    Game = models.ForeignKey(Game)
    User = models.ForeignKey(User)

What happened

The following error occurs during migration

Traceback (most recent call last):
    Game = models.ForeignKey(Game)
TypeError: __init__() missing 1 required positional argument: 'on_delete'

I got angry without on_delete.

Cause

The cause is simple, but it is because on_delete is not specified in the argument.

By the way, it seems that it became mandatory from Django 2.0.

When I actually look at the official release notes, it says like this.

The on_delete argument for ForeignKey and OneToOneField is now required in models and migrations.

** Translate ** The ForeignKey and OneToOneField on_delete arguments are now required for modeling and migration.

Django 2.0 Release Notes

How to use on_delete

So what is on_delete?

This is the behavior when the referenced object is deleted.

https://docs.djangoproject.com/ja/3.0/ref/models/fields/

Below is a brief summary based on the official reference

on_delete=models.CASCADE Ze that deletes all foreign key data!

on_delete=models.PROTECT Ze that makes it impossible to delete!

on_delete=models.SET_NULL If it's deleted, I'll put NULL instead! Set null = true when using this Ze!

on_delete=models.SET_DEFAULT Ze to replace the default value when deleted!

on_delete=models.SET(hogehoge) If it is deleted, you can call a function etc. and enter an arbitrary value Ze!

on_delete=models.DO_NOTHING Do nothing Ze!

Summary

It's a rich migration life with on_delete that suits your purpose Ze!

Recommended Posts

If you specify a foreign key in Django's model change, you need to specify on_delete.
If you specify a foreign key in Django's model change, you need to specify on_delete.
If you want to display values using choices in a template in a Django model
How to change the appearance of unselected Foreign Key fields in Django's ModelForm
How to specify a schema in Django's database settings
Understand Python yield If you put yield in a function, it will change to a generator
What to do if you get a minus zero in Python
If you want to assign csv export to a variable in python
Check if you can connect to a TCP port in Python
What is on_delete used in django's model?
What to do if you get a "No versions found" error in pipenv
Make any key the primary key in Django's model
If you encounter a "Unicode Decode Error" in Python
What to do if a UnicodeDecodeError occurs in pip
When you want to plt.save in a for statement
What to do if you get a Cannot retrieve metalink for repository error in yum
[python] How to check if the Key exists in the dictionary
Why do you add a main ()-if statement in Python?
A story about how to specify a relative path in python.
Change the standard output destination to a file in Python
What to do if pip gives a DistributionError in Homebrew
How to import a file anywhere you like in Python
What to do if you get "coverage unknown" in Coveralls
What to do if a 0xC0000005 error occurs in tf.train.start_queue_runners ()
How to check if a value exists in an enum
What to do if you can't log in as root
What to do if you lose your EC2 key pair
What to do if you get angry with'vertices' must be a 2D list ... in matplotlib arrow
[Django] What to do if the model you want to create has a large number of fields
What to do if you get a must override `get_config` error when trying to model.save in Keras