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

纵然是瞬间 提交于 2019-11-27 00:16:31

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:

https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey

EDIT:

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,
# ...

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.

Up until Django 1.9 a model would look like the following:

from django.db import models
class Article(models.Model):
    category = models.ForeignKey(Category)
    title =  models.CharField(max_length=55)
    # ...

    def __str__(self):
        return self.title

ForeignKey is a Django field for defining a many-to-one relationship.

Up until Django 1.9 the ForeignKey field required a single argument: the model to map to.

Since Django 2.0 the ForeignKey field requires two positional arguments:

1- the model to map to

2-the on_delete argument

For a quick fix of “missing 1 required positional argument: on_delete” update the model:

from django.db import models
class Article(models.Model):
    category = models.ForeignKey('Category', on_delete=models.PROTECT)
    title =  models.CharField(max_length=55)
    # ...

    def __str__(self):
        return self.title

After fixing ForeignKey you’ll be able to run migrations without any trouble:

python manage.py migrate

Thanks to Valentino

Javed Gouri

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.

Bala Kiswe

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)

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.

Here are available options if it helps anyone for on_delete

CASCADE, DO_NOTHING, PROTECT, SET, SET_DEFAULT, SET_NULL

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!