Reputation: 9
I'm getting this error:
user = models.OneToOneField(User) TypeError: init() missing 1 required positional argument: 'on_delete'
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class UserProfileInfo(models.Model):
# creating relationship
user = models.OneToOneField(User)
# additional attributes
portfolio = models.URLField(blank=True)
picture = models.ImageField(upload_to='profile_pics', blank=True)
def __str__(self):
return self.user.username
Upvotes: 0
Views: 562
Reputation: 31
put 'on_delete = models.CASCADE' in the constructor
what it does: when you will delete the user object as you have referenced it in the user field of your model. it will also delete the model object of UserProfileInfo for taht particular user.
Upvotes: 1
Reputation: 3272
A similar question is answered here Getting TypeError: __init__() missing 1 required positional argument: 'on_delete' when trying to add parent table after child table with entries
Basically following should fix url problem
From Django 2.0 on_delete is required:
user = models.OneToOneField(User, on_delete=models.CASCADE)
Upvotes: 1
Reputation: 476594
As the error indicates, you need to specify what should happen, given the object to which you refer is removed, by the on_delete=
parameter [Django-doc]. For example:
class UserProfileInfo(models.Model):
# creating relationship
user = models.OneToOneField(User, on_delete=models.CASCADE)
# additional attributes
portfolio = models.URLField(blank=True)
picture = models.ImageField(upload_to='profile_pics', blank=True)
def __str__(self):
return self.user.username
The options here are:
CASCADE
Cascade deletes. Django emulates the behavior of the SQL constraint
ON DELETE CASCADE
and also deletes the object containing theForeignKey
.
Model.delete()
isn't called on related models, but thepre_delete
andpost_delete
signals are sent for all deleted objects.
PROTECT
Prevent deletion of the referenced object by raising
ProtectedError
, a subclass ofdjango.db.IntegrityError
.
SET_NULL
Set the
ForeignKey
null; this is only possible ifnull
isTrue
.
SET_DEFAULT
Set the
ForeignKey
to itsdefault
value; a default for theForeignKey
must be set.
SET()
Set the
ForeignKey
to the value passed toSET()
, or if a callable is passed in, the result of calling it. In most cases, passing a callable will be necessary to avoid executing queries at the time yourmodels.py
is imported (...)
DO_NOTHING
Take no action. If your database backend enforces referential integrity, this will cause an
IntegrityError
unless you manually add anSQL ON DELETE
constraint to the database field.
Upvotes: 1