我正在创建一个用户可以在其中注册其他用户的表单,并且需要自动将当前用户的外键(组织)传递给正在创建的新用户。
我是Django的新手,但我相信问题在于表单中的以下行:
“ user.organization = self.instance.organization”
型号
class Organization(models.Model):
name = models.CharField(max_length=255)
address = models.CharField(max_length=255)
city = models.CharField(max_length=255)
state = models.CharField(max_length=255)
zip = models.CharField(max_length=255)
class User(AbstractUser):
username = None
email = models.EmailField(_('email address'), unique=True)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, blank=True, null=True)
client = models.ForeignKey(Client, on_delete=models.CASCADE, blank=True, null=True)
user_type = models.CharField(max_length=255, choices=(('org_admin', 'Admin'),('org_user','User'),('client_admin','Client Admin'),('client_user','Client User')))
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
查看
def register(request):
if request.method == 'POST':
form = UserOrgCreationForm(request.POST)
if form.is_valid():
form.save()
email = form.cleaned_data.get('email')
messages.success(request, f'User has been created')
# return redirect('login')
else:
form = UserOrgCreationForm()
return render(request, 'users/register.html', {'form': form})
表格
class UserOrgCreationForm(UserCreationForm):
password1 = None
password2 = None
class Meta:
model = User
fields = ('email', 'first_name', 'last_name', 'user_type')
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_unusable_password()
user.organization = self.instance.organization
if commit:
user.save()
return user
我需要新用户继承当前用户的组织。
答案 0 :(得分:1)
由于您的表单不基于现有实例(创建表单实例时不会传递),因此保存时无法引用该表单。
您可以按照以下方式做您想做的事
def register(request):
if request.method == 'POST':
form = UserOrgCreationForm(request.POST)
if form.is_valid():
new_user = form.save(commit=False)
new_user.organization = request.user.organization
new_user.save()
email = form.cleaned_data.get('email')
messages.success(request, f'User has been created')
# return redirect('login')
else:
form = UserOrgCreationForm()
return render(request, 'users/register.html', {'form': form})
答案 1 :(得分:0)
您可以利用commit=False
parameter。在第二次调用.save()
之前,不会将表单保存到数据库中。
def register(request):
if request.method == 'POST':
form = UserOrgCreationForm(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.organization = request.user.organization
obj.save()
email = form.cleaned_data.get('email')
messages.success(request, f'User has been created')
# return redirect('login')
else:
form = UserOrgCreationForm()
return render(request, 'users/register.html', {'form': form})
答案 2 :(得分:0)
您需要通过保存表单访问request.user.organisation
表单。
class UserOrgCreationForm(UserCreationForm):
password1 = None
password2 = None
class Meta:
model = User
fields = ('email', 'first_name', 'last_name', 'user_type')
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_unusable_password()
user.organization = current_user.organization
if commit:
user.save()
return user
def __init__(self,*args,**kwargs):
self.current_user = kwargs.pop("current_user", None)
super(UserOrgCreationForm,self).__init__(*args,**kwargs)
从视图中看:
def register(request):
form = UserOrgCreationForm(request.POST or None, current_user=request.user)
if form.is_valid():
form.save()
email = form.cleaned_data.get('email')
messages.success(request, f'User has been created')
# return redirect('login')
return render(request, 'users/register.html', {'form': form})