Solve : typeerror: __init__() missing 1 required positional argument: ‘on_delete’

Last Updated On Friday 27th May 2022

typeerror: init() missing 1 required positional argument: ‘on_delete’

  • This is ForeignKey, an error that occurs when using a Django model.
  • In Django 2.0 or later, two parameters are input.
  • AutoOneToOneField > OneToOneField > Inherited in order of ForeignKey class
  • Docs : ForeignKey

Solution: on_delete Write additional parameters

	class Profile(models.Model):
    user = AutoOneToOneField(User, primary_key=True, on_delete=models.CASCADE)
	

init() missing 1 required positional argument: ‘on_delete’

	class Guest(models.Model):
    event = models.ForeignKey (Event) # ID de conferencia asociada
     realname = models.CharField (max_length = 64) #Name
     phone = models.CharField (max_length = 16) # Número de teléfono móvil
     email = models.emailField () #Email
     sign = models.BooleanField () # Estado de inicio de sesión
     create_time = models.DateTimeField (auto_now = True) #Creation time (obtiene la hora actual automáticamente)
	
	TypeError:__init__() missing 1 required positional argument: 'on_delete'
	
  • models.CASCADE: Prevents deletion of referenced objects by raising RestrictedError (subclass of Django.DB) (prevents integrity errors)
  • models.RESTRICT: Objects referenced through the CASCADE relationship are also deleted.
  • models.PROTECT: When the value viewed by the ForeignKeyField is deleted, ProtectedError is raised to prevent deletion.
  • models.SET_NULL: When the value viewed by the ForeignKeyField is deleted, the ForeignKeyField value is set to NULL.
  • models.DO_NOTHING: Takes no action when the value the ForeignKeyField looks at is deleted.

References

__init__() missing 1 required positional argument:
foreignkey.__init__() missing 1 required positional argument: 'on_delete'
build() missing 1 required positional argument: 'input_shape'
__init__() missing 1 required positional argument
__init__() missing 1 required positional argument: 'on_delete'
typeerror: foreignkey.__init__() missing 1 required positional argument: 'on_delete'
typeerror: __init__() missing 1 required positional argument:
__init__() missing 1 required positional argument: 'scheme'