当我试图保存用户时,我在 django 中有 'NoneType' 对象没有属性 'groups'
I had 'NoneType' object has no attribute 'groups' in django when I tried to save an user
我是 django 的新手,我在 Admin 中有三个组:Medico、Paciente、Administrador。我只是有错误,AttributeError at /registro/ 'NoneType' object has no attribute 'groups',但我的表单正在保存所有数据
# Registro del Sistema
def home_registro(request):
if request.method == 'POST':
formulario = PacienteFormulario(request.POST)
if formulario.is_valid():
paciente = formulario.save()
grupo = Group.objects.get(name='Paciente')
paciente.groups.add(grupo)
return render(request, 'home/home_confirmacion.html')
else:
formulario = PacienteFormulario()
context = {
'formulario': formulario
}
return render(request, 'home/home_registro.html', context)
form.py:
class PacienteFormulario(UserCreationForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')
def save(self, commit = True):
user = super().save(commit = False)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
if commit:
user.save()
错误:
line 35, in home_registro
paciente.groups.add(grupo)
AttributeError: 'NoneType' object has no attribute 'groups'
我希望有人能帮助我。谢谢
您没有return save
方法中的用户:
class PacienteFormulario(UserCreationForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')
def save(self, commit = True):
user = super().save(commit = False)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
if commit:
user.save()
<b>return user</b>
但通常你不需要在这里覆盖 save(…)
,通常 ModelForm
- UserCreationForm
是 ModelForm
- 会处理这个。
我是 django 的新手,我在 Admin 中有三个组:Medico、Paciente、Administrador。我只是有错误,AttributeError at /registro/ 'NoneType' object has no attribute 'groups',但我的表单正在保存所有数据
# Registro del Sistema
def home_registro(request):
if request.method == 'POST':
formulario = PacienteFormulario(request.POST)
if formulario.is_valid():
paciente = formulario.save()
grupo = Group.objects.get(name='Paciente')
paciente.groups.add(grupo)
return render(request, 'home/home_confirmacion.html')
else:
formulario = PacienteFormulario()
context = {
'formulario': formulario
}
return render(request, 'home/home_registro.html', context)
form.py:
class PacienteFormulario(UserCreationForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')
def save(self, commit = True):
user = super().save(commit = False)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
if commit:
user.save()
错误:
line 35, in home_registro
paciente.groups.add(grupo)
AttributeError: 'NoneType' object has no attribute 'groups'
我希望有人能帮助我。谢谢
您没有return save
方法中的用户:
class PacienteFormulario(UserCreationForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')
def save(self, commit = True):
user = super().save(commit = False)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
if commit:
user.save()
<b>return user</b>
但通常你不需要在这里覆盖 save(…)
,通常 ModelForm
- UserCreationForm
是 ModelForm
- 会处理这个。