Django - 将工作人员设置为新用户的默认值
Django - setting staff member as default for new users
各位!
我正在尝试为新用户设置默认 staff_member,但我找不到任何解决方案。我真的需要帮助。我的代码如下。
型号
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
表格
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']
观看次数
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())
您设置 .instance
的 .is_staff
属性包裹在 form
中,所以:
def cadastro(request):
if request.method == 'POST':
form = ParticipanteForm(request.POST)
if form.is_valid():
form<b>.instance.is_staff = True</b>
form.save()
return redirect('dashboard')
else:
form = ParticipanteForm()
return render(request, 'cadastro.html', locals())
您的 ParticipanteForm
也适用于 User
模型,而不是 Participante
模型。如果将 Participante
设置为 用户模型 ,则可以使用 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 = <b>get_user_model()</b>
fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2']
然而,要使 Participante
成为用户模型还需要一些额外的工作。 Django 在 substituting a custom user model [Django-doc].
上有一个主题
我认为这里的问题是您的 UserCreationForm
指向 User
模型而不是您的自定义 Participante
模型。因此,用户没有被保存在您期望的 table 中。
在 settings.py
中,将 Participante
模型设置为您的用户模型(您的 Participante
模型也必须继承 AbstractUser
以保持 User
模型的方法等
# 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
然后在您的表单中,使用 get_user_model()
指向您的 AUTH_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']```
各位!
我正在尝试为新用户设置默认 staff_member,但我找不到任何解决方案。我真的需要帮助。我的代码如下。
型号
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
表格
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']
观看次数
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())
您设置 .instance
的 .is_staff
属性包裹在 form
中,所以:
def cadastro(request):
if request.method == 'POST':
form = ParticipanteForm(request.POST)
if form.is_valid():
form<b>.instance.is_staff = True</b>
form.save()
return redirect('dashboard')
else:
form = ParticipanteForm()
return render(request, 'cadastro.html', locals())
您的 ParticipanteForm
也适用于 User
模型,而不是 Participante
模型。如果将 Participante
设置为 用户模型 ,则可以使用 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 = <b>get_user_model()</b>
fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2']
然而,要使 Participante
成为用户模型还需要一些额外的工作。 Django 在 substituting a custom user model [Django-doc].
我认为这里的问题是您的 UserCreationForm
指向 User
模型而不是您的自定义 Participante
模型。因此,用户没有被保存在您期望的 table 中。
在 settings.py
中,将 Participante
模型设置为您的用户模型(您的 Participante
模型也必须继承 AbstractUser
以保持 User
模型的方法等
# 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
然后在您的表单中,使用 get_user_model()
AUTH_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']```