Tag Archives: Django error

[Solved] TypeError: __init__() missing 1 required positional argument: ‘on_delete’

TypeError: __init__() missing 1 required positional argument:’on_delete’ solution

 

When executing python manage.py makemigrations, an error occurs: TypeError: init() missing 1 required positional argument:’on_delete’

solution:

When defining a foreign key, you need to add on_delete=;
that is: contract = models.ForeignKey(Contract, on_delete=models.CASCADE)

The reasons are as follows:

After django is upgraded to 2.0, when the table is associated with the table, the on_delete parameter must be written, otherwise an exception will be reported:
TypeError: init() missing 1 required positional argument:’on_delete’

 

on_delete=None, # When deleting data in the associated table, the behavior of the current table and its associated field
on_delete=models.CASCADE, # Delete associated data, and delete associated with it
on_delete=models.DO_NOTHING, # Delete associated data, nothing Don’t do
on_delete=models.PROTECT, # Delete associated data and raise an error ProtectedError
# models.ForeignKey(‘Associated table’, on_delete=models.SET_NULL, blank=True, null=True)
on_delete=models.SET_NULL, # Delete associated data , The value associated with it is set to null (provided that the FK field needs to be set to be nullable, and one pair is
treated together ) # models.ForeignKey(‘associated table’, on_delete=models.SET_DEFAULT, default=’default value’)
on_delete=models .SET_DEFAULT, # Delete the associated data, and set the associated value to the default value (provided that the FK field needs to set the default value, one pair is the same)
on_delete=models.SET, # Delete associated data,
a. Set the associated value To specify the value, set: models.SET (value)
b. Set the associated value to the return value of the executable object, set: models.SET (executable object)

Since many-to-many (ManyToManyField) has no on_delete parameter, the above is only for foreign keys (ForeignKey) and one-to-one (OneToOneField)