Python 如果用户创建的是Django中的特定组,如何使信号运行?
我有一个模型客户端,它使用@receiver信号在创建用户时更新其字段,因此它创建了一个客户端配置文件 如果创建的用户属于客户端组,是否有方法仅运行此操作?因为如果在Employees组中创建了用户,我不想创建配置文件 这是在“客户端”组中创建客户端的视图:Python 如果用户创建的是Django中的特定组,如何使信号运行?,python,django,django-3.0,Python,Django,Django 3.0,我有一个模型客户端,它使用@receiver信号在创建用户时更新其字段,因此它创建了一个客户端配置文件 如果创建的用户属于客户端组,是否有方法仅运行此操作?因为如果在Employees组中创建了用户,我不想创建配置文件 这是在“客户端”组中创建客户端的视图: @login_required(login_url='./accounts/login/') def signup(request): if request.method == 'POST': form = Sign
@login_required(login_url='./accounts/login/')
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save() # this creates the user with first_name, email and last_name as well!
group = Group.objects.get(name='Clients')
user.groups.add(group)
user.refresh_from_db() # load the profile instance created by the signal
user.clients.address = form.cleaned_data.get('address')
user.clients.city = form.cleaned_data.get('city')
user.clients.postal = form.cleaned_data.get('postal')
user.clients.nif = form.cleaned_data.get('nif')
user.clients.mobile = form.cleaned_data.get('mobile')
return redirect('clients')
else:
form = SignUpForm()
return render(request, 'backend/new_client.html', {'form': form})
在无信号的视图中执行此操作:
@login_required(login_url='./accounts/login/')
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save()
group = Group.objects.get(name='Clients')
user.groups.add(group)
client = Client.objects.create(
user=user,
address=form.cleaned_data.get('address')
city=form.cleaned_data.get('city')
postal=form.cleaned_data.get('postal')
nif=form.cleaned_data.get('nif')
mobile=form.cleaned_data.get('mobile')
)
return redirect('clients')
else:
form = SignUpForm()
return render(request, 'backend/new_client.html', {'form': form})
然后您可以选择在user=form.save下移动所有代码,保存在表单本身中我假设它是一个自定义模型表单:
# forms.py
class SignUpForm(models.Form):
# your existing code here
def save(self):
# NB if you're still using py2 you'll need
# `user = super(SignUpForm, self).save()` instead
user = super().save()
group = Group.objects.get(name='Clients')
user.groups.add(group)
cleaned_data = self.cleaned_data
client = Client.objects.create(
user=user,
address=cleaned_data.get('address')
city=cleaned_data.get('city')
postal=cleaned_data.get('postal')
nif=cleaned_data.get('nif')
mobile=cleaned_data.get('mobile')
)
return user
你的观点是:
@login_required(login_url='./accounts/login/')
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
return redirect('clients')
else:
form = SignUpForm()
return render(request, 'backend/new_client.html', {'form': form})
这两个选项都是正确的,并且在功能上是等效的,但第二个选项更易于维护——首先是因为表单比不需要创建请求对象的视图更易于测试,还因为它将整个域逻辑封装在表单的同一位置,而不是分散在表单和视图之间。唯一的缺点是您无法将commit=False参数传递给form.save,但是,由于此表单显然没有其他用途,您无论如何也不会使用此功能。您不需要在此处发出信号-只需在视图中创建客户端,或者在form.save方法中创建更好的IMHO即可。@brunodesshuilliers我如何在form.save中创建它?我是Django的新手,使用信号是我唯一能做到这一点的方法。真是太棒了。当时我走错了路。在您第一次发表评论后,我开始尝试找出视图,但我缺少client=client.objects.create。。。。非常感谢。请注意,您甚至不需要将客户机绑定到本地客户机变量-您只需使用普通的Client.objects.create。。。。一旦创建,它就可以作为user.client使用。哦,是的,太棒了!
@login_required(login_url='./accounts/login/')
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
return redirect('clients')
else:
form = SignUpForm()
return render(request, 'backend/new_client.html', {'form': form})