[Django]-Getting TypeError: __init__() missing 1 required positional argument: 'on_delete' when trying to add parent table after child table with entries

229πŸ‘

βœ…

You can change the property categorie of the class Article like this:

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

and the error should disappear.

Eventually you might need another option for on_delete, check the documentation for more details:

Arguments β€” Model field reference β€” Django documentation

As you stated in your comment, that you don’t have any special requirements for on_delete, you could use the option DO_NOTHING:

# ...
on_delete=models.DO_NOTHING,
# ...
πŸ‘€cezar

50πŸ‘

Since Django 2.x, on_delete is required.

Django Documentation

Deprecated since version 1.9: on_delete will become a required
argument in Django 2.0. In older versions it defaults to CASCADE.

17πŸ‘

Since Django 2.0 the ForeignKey field requires two positional arguments:

  1. the model to map to
  2. the on_delete argument
categorie = models.ForeignKey('Categorie', on_delete=models.PROTECT)

Here are some methods can used in on_delete

  1. CASCADE

Cascade deletes. Django emulates the behavior of the SQL constraint ON DELETE CASCADE and also deletes the object containing the ForeignKey

  1. PROTECT

Prevent deletion of the referenced object by raising ProtectedError, a subclass of django.db.IntegrityError.

  1. DO_NOTHING

Take no action. If your database backend enforces referential integrity, this will cause an IntegrityError unless you manually add an SQL ON DELETE constraint to the database field.

you can find more about on_delete by reading the documentation.

14πŸ‘

From Django 2.0 on_delete is required:

user = models.OneToOneField(User, on_delete=models.CASCADE)

It will delete the child table data if the User is deleted. For more details check the Django documentation.

πŸ‘€Javed Gouri

4πŸ‘

If you are using foreignkey then you have to use β€œon_delete=models.CASCADE” as it will eliminate the complexity developed after deleting the original element from the parent table. As simple as that.

categorie = models.ForeignKey('Categorie', on_delete=models.CASCADE)
πŸ‘€Bala Kiswe

4πŸ‘

Post Django version 1.9, on_delete became a required argument, i.e. from Django 2.0.

In older versions, it defaults to CASCADE.

So, if you want to replicate the functionality that you used in earlier versions. Use the following argument.

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

This will have the same effect as that was in earlier versions, without specifying it explicitly.

Official Documentation on other arguments that go with on_delete

πŸ‘€Optider

3πŸ‘

Here are available options if it helps anyone for on_delete

CASCADE, DO_NOTHING, PROTECT, SET, SET_DEFAULT, SET_NULL

πŸ‘€Tarun Behal

3πŸ‘

If you don’t know which option to enter the params.
Just want to keep the default value like on_delete=None before migration:

on_delete=models.CASCADE

This is a code snippet in the old version:

if on_delete is None:
    warnings.warn(
        "on_delete will be a required arg for %s in Django 2.0. Set "
        "it to models.CASCADE on models and in existing migrations "
        "if you want to maintain the current default behavior. "
        "See https://docs.djangoproject.com/en/%s/ref/models/fields/"
        "#django.db.models.ForeignKey.on_delete" % (
            self.__class__.__name__,
            get_docs_version(),
        ),
        RemovedInDjango20Warning, 2)
    on_delete = CASCADE
πŸ‘€HoangYell

2πŸ‘

"models.ForeignKey()" and "models.OneToOneField" must have "on_delete" since Django 2.0. For example below:

"models.ForeignKey()":

categories = models.ForeignKey('Category', on_delete=models.CASCADE)

"models.OneToOneField":

categories = models.OneToOneField('Category', on_delete=models.PROTECT)

0πŸ‘

Had a similar problem that resolved by adding both these two parameters to ForeignKey:
null=True, on_delete=models.SET_NULL

πŸ‘€Nir Tsabar

0πŸ‘

For me the package manager was resolving djangorestframework to a lower version and the authtoken model did not have the on_delete. All of my models were good. You can run django.apps.apps.get_models() to get a list of all your models to see what libraries create their own models and might be the issue.

πŸ‘€anishtain4

-4πŸ‘

This worked for me
pip install django-csvimport --upgrade

Leave a comment