SaidAkh
SaidAkh

Reputation: 1871

How to implement "email_user" method in custom user model?

Trying to use django-registration module with custom user model that extends AbstractBaseUser.

This is a form, I use for the registration:

from django.contrib.auth import get_user_model
from registration.forms import RegistrationForm

class UserCreateForm(RegistrationForm):
    class Meta:
        fields = ("username", "email", "password1", "password2")
        model = get_user_model()

This is the view:

class Register(RegistrationView):
    form_class = forms.UserCreateForm

This is my registration url:

url(r'^accounts/register/$', views.Register.as_view(), name='registration_register'),

Edit:

Django does not send confirmation email, and browser throws an error

'User' object has no attribute 'email_user'

Edit:

I understand I have to implement email_user method in my custom user model. How can I do this?

Upvotes: 0

Views: 2428

Answers (2)

Brylie Christopher Oxley
Brylie Christopher Oxley

Reputation: 1872

Here is an example of the email_user from the Django AbstractUser class.

from django.contrib.auth.models import AbstractBaseUser
from django.core.mail import send_mail

class CustomUser(AbstractBaseUser):
    ...

    def email_user(self, subject, message, from_email=None, **kwargs):
        """Send an email to this user."""
        send_mail(subject, message, from_email, [self.email], **kwargs)

Upvotes: 0

SaidAkh
SaidAkh

Reputation: 1871

I just needed to add email_user method to my custom user model

    def email_user(self, *args, **kwargs):
    send_mail(
        '{}'.format(args[0]),
        '{}'.format(args[1]),
        '{}'.format(args[2]),
        [self.email],
        fail_silently=False,
    )

Upvotes: 3

Related Questions