kiril
kiril

Reputation: 5212

Duplicate (and empty) user is created using regular account signup

I set up django-allauth in my project and social accounts work great. However, when I create user using regular acoounts it saves the user twice: One with username and another without username (this is empty username). Thus, next time I try to create a user it raises an Exception because username must be unique and it is duplicated, the empty username.

It set receivers for every signal and print logs when each of them are handled. However, I could only fence the problem between the populate_username signal and the email_confirmation_sent signal

Upvotes: 0

Views: 232

Answers (1)

kiril
kiril

Reputation: 5212

After diving into the source code, and sprinkle logs everywhere I found the problem was caused by the custom Signup Model Form I use (I could have figured it out but it wasn't easy).

django-allauth manually saves the user, so in my case the form model default save method does it again and causes the bug.

As pointed out in the docs:

This class (the ACCOUNT_SIGNUP_FORM_CLASS) should implement a def signup(self, request, user) method, where user represents the newly signed up user.

However, the appreciation is quite vague and (at least in my case) it is easy to think that the ACCOUNT_SIGNUP_FORM_CLASS could be a form (whatever the kind).

So, I found two solutions:

  1. Replace the model form with a regular form. However, it is not clearly explained in the docs.
  2. Override the ModelForm's save method and skip the call to parent's save method

I implemented the second one, and the form end up as follows:

class SignupForm(UserCreationForm):

    class Meta:
        model = get_user_model()
        fields = ('first_name', 'last_name', 'email')

    def save(self, commit=True):
        return None

Upvotes: 1

Related Questions