For a slew of reasons that detailing will make this post too long, I'm using a custom user model, alongside django-registration, and guardian.
Everything is working quite alright, but I'm trying to make it so every user registered is automatically set to active, so "they" (right now it's only me emulating users) can login instantly. The way I did so far is by manually setting that field to "1" (True) in the SQL table.
It seems that no matter how I arrange things, the is_active
flag is always False
until I change it manually in SQL, even though I'm printing it to my console and it's showing True when I register.
I did get it to work by changing user.save()
to user.save(commit=True)
, that does make the is_active
flag True
and allows the user to log-in, but it always throws an error that says:
TypeError: save() got an unexpected keyword argument 'commit'
Of course, this is all for testing purposes and I do intend to introduce email verification, but I'm very intrigued on why I'm not able to simply set the flag and save()
.
Here's my register/forms.py
:
class GeneralUserForm(UserCreationForm): business_name = forms.CharField(required=True) class Meta: model = GeneralUser fields = ['username', 'email', 'password1','password2', 'business_name'] # TODO: only create all objects if no error is found def save(self, commit=True): user = super(GeneralUserForm, self).save(commit=False) user.set_password(self.cleaned_data["password1"]) # TODO: create permissions, including groups. if commit: user.is_active = True # remove before deployment print(user.is_active) user.save() # LINE I CHANGED business = Business.objects.create(name=self.cleaned_data['business_name'], owner=user) business.save() mng_grp = create_perms_for_biz(business, user) user.groups.add(mng_grp) return user
And my register/models.py
:
class GeneralUser(AbstractUser, GuardianUserMixin): username = models.CharField( ('username'), max_length=150, unique=True, help_text=('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'), error_messages={'unique': ("A user with that username already exists."), }, ) password = models.CharField(max_length=100) email = models.EmailField(('email address'), blank=True) is_active = models.BooleanField( ('active'), default=True, help_text=('Designates whether this user should be treated as active. ''Unselect this instead of deleting accounts.' ), ) objects = UserManager() date_joined = models.DateTimeField(('date joined'), default=timezone.now) business_name = models.CharField(('business name'), max_length=150, blank=True) USERNAME_FIELD = 'username' def email_user(self, subject, message, from_email=None, **kwargs):""" Sends an email to this User.""" send_mail(subject, message, from_email, [self.email], **kwargs)def get_custom_anon_user(User): return User( username='AnonymousUser', )