Reputation: 1489
In pure Django I would just do it like:
from django.contrib.auth.models import User
user = User.objects.create_user(username=email, email=email)
But django allauth comes with this EmailAdress
stuff. Do I just have to create one of these too and then I'm fine?
from allauth.account.models import EmailAddress
EmailAddress.objects.create(user=user, email=email, primary=True, verified=False)
I don't want to break some django allauth logic and the existing adapter methods doesn't suit my needs.
EDIT: replaced setup_user_email
with EmailAddress
EDIT2: replaced add_email
with create
, want to set primary=True
Upvotes: 22
Views: 4097
Reputation: 1
It was so eerie to come across a question that so precisely described my own usecase! I couldn't find an answer anywhere, so I did a bit of digging in the django-allauth repository. Here's my solution -
from allauth.account.forms import SignupForm
data = {
'email': 'john@leapoffaith.com',
'password1': 'Password@99',
'password2': 'Password@99',
}
form = SignupForm(data)
if form.is_valid():
form.save(request)
Use this code anywhere in your views.py file and you should be good to go.
Upvotes: 0
Reputation: 3795
The canonical way to create a user seems to be stuck in the SignupForm.save()
method:
Edit: It's actually one level higher in the SignupView
, which first calls the SignupForm.save()
and then calls account.utils.complete_signup()
, which sends the user_signed_up
signal:
Upvotes: 4