Solving Python Error – TypeError: __init__() missing 1 required positional argument: ‘on_delete’

TypeError: __init__() missing 1 required positional argument: ‘on_delete’ is an error which happens when you mishandle classes in your sqlite database.

In this article I am going to explain what happens when you get this error and how you can solve it with a main solution, we will also explore other solutions which can possibly solve the issue.

Exploring the TypeError: __init__() missing 1 required positional argument: ‘on_delete’

First we should understand that this error occurs when we mishandle classes in an sqlite database.

Let us start by checking out the message of the error. Bellow is the code which reproduces the error.

Please make sure the message of the error you are getting is not very different from this one, you do not want to waste your time trying to solve an error you are not facing.

                                                                       #
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
    utility.execute()
.....
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "C:\Users\lislis\Django\mon_site\blog\models.py", line 6, in <module>
    class Article(models.Model):
.....
TypeError: __init__() missing 1 required positional argument: 'on_delete'
                                                                       #

The error happens after running the code bellow.

                                                                       #
python manage.py makemigrations rubie.py
                                                                       #

Bellow, I will present a solution that has worked for me and for other developers.

Solution : use on_delete=models.CASCADE

First, we should understand that you can change the property categorie of the class Article using the code bellow, the error should be gone after that :

                                                                       #
categorie = models.ForeignKey(
'Categorie',
on_delete=models.CASCADE, )
                                                                       #

You can choose the option DO_NOTHING: for on_delete. That is only if you do not have any special requirements for it.

                                                                       #
on_delete=models.DO_NOTHING,
                                                                       #

Keep in mind that both models.ForeignKey() and models.OneToOneField must have on_delete in the latest versions of Django.

                                                                       #
categories = models.ForeignKey('Category', on_delete=models.CASCADE)     # for "models.ForeignKey()".
categories = models.OneToOneField('Category', on_delete=models.PROTECT)  # for "models.OneToOneField".
                                                                       #

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

This is the end of our article, this error could be confusing but with a little bit of investigating the error could be solved easily by renaming your files, thank you for reading. To support us consider donating to our Kofi account above. Keep coding, keep learning and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/