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)
Read More:
- Django Render home error: TypeError at /render() missing 1 required positional argument: ‘template_name‘
- [Solved] TypeError: xx takes 1 positional argument but 4 were given
- [Solved] Python Error: positional argument follows keyword argument
- [Solved] Jupyter notebook: TypeError: __init__() got an unexpected keyword argument ‘io_loop’
- [Solved] Python Error: TypeError: write() argument must be str, not bytes
- Python Run Error: TypeError: hog() got an unexpected keyword argument ‘visualise‘”
- [Solved] main.py: error: the following arguments are required:
- [Solved] train.py: error: the following arguments are required: –XXXX
- [Solved] error: the following arguments are required (Default parameters are set)
- Python Redis: How to batch fuzzy Delete Keys
- [Solved] PyTorch Caught RuntimeError in DataLoader worker process 0和invalid argument 0: Sizes of tensors mus
- [Solved] error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘copyMakeBorder‘
- [Solved] mnn Import Error: initMNN: init numpy failed
- [Solved] Pdfplumber Read PDF Sheet Error: AttributeError: function/symbol ‘ARC4_stream_init‘ not found in library
- [Solved] Pytroch warning: “Argument interpolation should be of type InterpolationMode instead of int. “
- Python Pandas Typeerror: invalid type comparison
- Tensor for argument #2 ‘mat1‘ is on CPU, but expected it to be on GPU (while checking arguments for
- ‘c’ argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapp
- Python: How to Delete Empty Files or Folders in the Directory
- [Solved] TypeError: Object of type ‘bytes’ is not JSON serializable