BeatrizGomes
BeatrizGomes

Reputation: 45

Django - setting staff member as default for new users

everybody!

I'm trying setting as default staff_member to new users, but I cannot find any solution. I really need help. My codes are below.

models

class Participante(models.Model):
    nome = models.CharField(max_length=100)
    cpf = models.CharField(max_length=13)
    email = models.EmailField()
    dt_criacao = models.DateTimeField(auto_now=True)
    is_staff = models.BooleanField(
        ('staff status'),
        default=True,
        help_text=('Designates whether the user can log into this admin site.'),
    )

    def __str__(self):
        return self.nome

forms

class ParticipanteForm(UserCreationForm):

    first_name = forms.CharField(max_length=100, label='Primeiro nome')
    last_name = forms.CharField(max_length=100, label='Último nome')
    class Meta:
        model = User
        fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2'] 

views

def cadastro(request):
    form = ParticipanteForm()

    if request.method == 'POST':
        form = ParticipanteForm(request.POST)
        
        if form.is_valid():
            #User(request, username=username, password=password1)
            form.save()
            return redirect('dashboard')

    return render(request, 'cadastro.html', locals())

Upvotes: 0

Views: 685

Answers (2)

Nicky
Nicky

Reputation: 316

I think the issue here is that your UserCreationForm is pointing to the User model and not your custom Participante model. Thus, users are not being saved in the table which you expect them to be.

In settings.py, set the Participante model to be your user model (your Participante model will also have to inherit AbstractUser to keep the User model's methods, etc.

Read: https://docs.djangoproject.com/en/3.1/topics/auth/customizing/#django.contrib.auth.models.AbstractUser

# settings.py
AUTH_USER_MODEL = 'your_app.models.Participante'
# your_app.models
from django.contrib.auth.models import AbstractUser

class Participante(AbstractUser):
    nome = models.CharField(max_length=100)
    cpf = models.CharField(max_length=13)
    email = models.EmailField()
    dt_criacao = models.DateTimeField(auto_now=True)
    is_staff = models.BooleanField(
        ('staff status'),
        default=True,
        help_text=('Designates whether the user can log into this admin site.'),
    )

    def __str__(self):
        return self.nome

Then in your form, point to your AUTH_USER_MODEL using get_user_model()

# forms.py
from django.contrib.auth import get_user_model

class ParticipanteForm(UserCreationForm):

    first_name = forms.CharField(max_length=100, label='Primeiro nome')
    last_name = forms.CharField(max_length=100, label='Último nome')
    class Meta:
        model = get_user_model()
        fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2']```

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476624

You set the .is_staff attribute of the .instance wrapped in the form, so:

def cadastro(request):
    if request.method == 'POST':
        form = ParticipanteForm(request.POST)
        if form.is_valid():
            form.instance.is_staff = True
            form.save()
            return redirect('dashboard')
    else:
        form = ParticipanteForm()
    return render(request, 'cadastro.html', locals())

Your ParticipanteForm also works with the User model, not the Participante model. If you set the Participante as the user model, you can work with the get_user_model() function [Django-doc]:

from django.contrib.auth import get_user_model

class ParticipanteForm(UserCreationForm):
    first_name = forms.CharField(max_length=100, label='Primeiro nome')
    last_name = forms.CharField(max_length=100, label='Último nome')
    
    class Meta:
        model = get_user_model()
        fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2']

It will however require some extra work to make Participante the user model. Django has a topic on substituting a custom user model [Django-doc].

Upvotes: 1

Related Questions