0

Why doing something so simple seems so complicated in Django? It started with setting up a user model where email is used to login, a pain... Now I am trying to save a user as user.is_active = False but it is saved as active. Any tips very welcome or any other guidance as Django has been quite a painful experience so far compared to Flask.

Note, I have also tried @receiver(pre_save, sender=get_user_model()) below.

views.py

...

from .models import Story, MyUser
from .forms import SignupForm, LoginForm

from django.dispatch import receiver
from django.db.models.signals import pre_save


@receiver(pre_save, sender=MyUser)
def set_new_user_inactive(sender, instance, **kwargs):
    if instance._state.adding is True:
        print("Creating Inactive User")
        instance.is_active = False
    else:
        print("Updating User Record")

...

def signup(request):
    if request.method == 'POST':
        form = SignupForm(request.POST)
        if form.is_valid():
            user = get_user_model()
            user.objects.create_user(
                form.cleaned_data['username'],
                form.cleaned_data['email'],
                form.cleaned_data['password'],
                )
        return HttpResponseRedirect(reverse('index'))
    else:
        form = SignupForm()
        return render(request, 'drillapp/signup.html', {'form': form})

...

models.py

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager

...

class MyUserManager(BaseUserManager):
    def create_user(self, username, email, password=None):
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
        )

        self.username = username
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, date_of_birth, password):
        user = self.create_user(
            email,
            password=password,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )

    objects = MyUserManager()

    USERNAME_FIELD = 'email'

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
        # The user is identified by their email address
        return self.email

    def __str__(self):              # __unicode__ on Python 2
        return self.email
cezar
  • 11,616
  • 6
  • 48
  • 84
M3RS
  • 6,720
  • 6
  • 37
  • 47
  • I would advise making use of the exiting [django-registration-redux](http://django-registration-redux.readthedocs.io/) – tread Oct 09 '17 at 09:08
  • 1
    It's not Django making the things co complicated, it is your approach making things complicated. It requires a positive attitude to get things done. – cezar Oct 09 '17 at 09:16
  • Maybe you should check this question: https://stackoverflow.com/questions/21514354/difference-between-abstractuser-and-abstractbaseuser-in-django – cezar Oct 09 '17 at 09:20
  • The above comments are helpful, thanks. – M3RS Oct 09 '17 at 09:26

1 Answers1

1

Why don't you do like:

class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    is_active = models.BooleanField(
        default=False
    )
cezar
  • 11,616
  • 6
  • 48
  • 84
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
  • Wouldn't it be better: `is_active = models.BooleanField(default=False)`? – cezar Oct 09 '17 at 09:04
  • I checked in the source code, and, indeed, it is `is_active = True` in the `AbstractBaseUser` class. But in the class `BaseUser`, which inherits from `AbstractBaseUser` it is `is_active = models.BooleanField(_('active'), default=False, help_text=_('...'))`. The previous comment was just a question, not a suggestion. – cezar Oct 09 '17 at 09:11
  • `is_active = False` worked for me. Had to restart the shell. Until then it kept showing me that `is_active` was `True`. – M3RS Oct 09 '17 at 11:27