在使用ModelForm创建的表单对象中设置用户字段 [英] Set user field in a form object created using ModelForm
问题描述
我有一个注册表单,当用户输入有效的注册数据时,它会将用户登录并将用户重定向到另一个表单以输入更多数据。这个新的表单包含更多关于用户的数据,并使用下面的ModelForm类创建:
class UserProfileAddForm(ModelForm):
class Meta:
model = UserProfile
exclude =('user')
和UserProfile模型看起来像:
class UserProfile(models.Model):
GRADE_YEAR_CHOICES =(
('FR','Freshman'),
('SO','Sophomore'),
('JR','Junior'),
('SR','高级'),
('GR','毕业生')
)
school = models.CharField(max_length = 64)
grade_year = models.CharField(
gpa = models.DecimalField(decimal_places = 2,max_digits = 6,blank = True,null = True)
user = models.ForeignKey(User,unique = True)
其中用户字段是用于Django用户对象的ForeignKey克拉。我很难将用户字段设置为发出请求的用户。这是我在views.py中试过的内容。
第一次尝试:
def more(request):
if request.method =='POST':
form = UserProfileAddForm(request.POST)
if form.is_valid ():
form.save(commit = False)
form.user = request.user $ b $ form.save()
return HttpResponseRedirect('/ register / success /')
else:
form = UserProfileAddForm()
variables = RequestContext(request,{$ b $'form':form
第二次尝试:
def more (request):
if request.method =='POST':
form = UserProfileAddForm(request.POST)
如果form.is_valid():
form.save(commit = False)
form.user = User.objects.filter(username = request.user)
form.save()
return Http ResponseRedirect('/ register / success /')
else:
form = UserProfileAddForm()
variables = RequestContext(request,{$ b $'form':form
} )
return render_to_response('registration / more.html',variables)
无论哪种方式I尝试它似乎并没有将数据保存到数据库。然而,它会将我重定向到成功页面,就像它保存成功一样。 ()返回正在保存的模型的一个实例。在这里你需要设置用户,然后保存该实例。
profile = form.save(commit = False)
profile.user = request.user
profile.save()
请注意,基于 request.user
的值从数据库获取用户是毫无意义的,因为 request.user
已经是正确的用户对象。
I have a registration form that when the user enters in valid registration data in it, it logs the user in and redirects the user to another form to enter more data. This new form contains more data about the user and is created using the ModelForm class below:
class UserProfileAddForm(ModelForm):
class Meta:
model = UserProfile
exclude = ('user')
and the UserProfile model looks like:
class UserProfile(models.Model):
GRADE_YEAR_CHOICES = (
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
('GR', 'Graduate')
)
school = models.CharField(max_length=64)
grade_year = models.CharField(max_length=2, choices=GRADE_YEAR_CHOICES)
gpa = models.DecimalField(decimal_places=2, max_digits=6, blank=True, null=True)
user = models.ForeignKey(User, unique=True)
Where the user field is a ForeignKey to Django's User object. I am having a hard time setting the user field to the user making the request. Here is what I have tried in my views.py
First attempt:
def more(request):
if request.method == 'POST':
form = UserProfileAddForm(request.POST)
if form.is_valid():
form.save(commit=False)
form.user = request.user
form.save()
return HttpResponseRedirect('/register/success/')
else:
form = UserProfileAddForm()
variables = RequestContext(request, {
'form': form
Second attempt:
def more(request):
if request.method == 'POST':
form = UserProfileAddForm(request.POST)
if form.is_valid():
form.save(commit=False)
form.user = User.objects.filter(username=request.user)
form.save()
return HttpResponseRedirect('/register/success/')
else:
form = UserProfileAddForm()
variables = RequestContext(request, {
'form': form
})
return render_to_response('registration/more.html', variables)
Either way I try it does not seem to save the data to the database. It will however redirect me to the success page as though it did save successfully.
form.save()
returns an instance of the model being saved. It's there that you need to set the user, then save that instance.
profile = form.save(commit=False)
profile.user = request.user
profile.save()
Note that getting the user from the database based on the value of request.user
is pointless, as request.user
already is the correct User object.
这篇关于在使用ModelForm创建的表单对象中设置用户字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!