在Django中,如何将窗体中的用户字段设置为当前登录的用户? [英] In Django, how do I set user field in form to the currently logged in user?
问题描述
我正在使用Django的内置ModelForm和CreateView。我的问题是Run for Office表单(换句话说,创建候选表单)允许用户选择数据库中的任何用户作为候选人。
我希望将Office for Office中的用户字段自动设置为当前登录的用户,并且该值被隐藏,所以登录用户不能将该字段的值更改为其他人。
views.py
class CandidateCreateView(CreateView):
model = Candidate
form_class = CandidateForm
template_name ='candidate_create.html'
def form_valid(self,form):
f = form.save(commit = False)
f.save()
返回超级(CandidateCreateView,self).form_valid(form)
forms.py
class CandidateForm(forms.ModelForm):
class Meta:
model = Candidate
models.py
class Candidate(models.Model ):
user = models.ForeignKey(UserProfile)
office = models.F ()
$ b $选择= models.ForeignKey(Election)
description = models.TextField()
def __unicode __(self):
return unicode(self。用户)
def get_absolute_url(self):
return reverse('candidate_detail',kwargs = {'pk':str(self.id)})
感谢阅读。现在我已经困在这个问题上,真的很感谢你的帮助。如果您需要更多信息,请联系我们。
-
从呈现的表单中删除用户字段(使用
字段
, https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#selecting-the-fields-to-use )class CandidateForm(forms.ModelForm):
class Meta:
model = Candidate
exclude = [user]
-
在创建视图中查找用户个人资料并设置用户字段
class CandidateCreateView(CreateView):
...
def form_valid(self,form)
candidate = form.save(commit = False)
candidate.user = UserProfile.objects.get(user = self.request.user)#在这里使用你自己的个人资料
candidate.save )
return HttpResponseRedirect(self.get_success_url())
Django and stackoverflow noob here. I'm making an election information app, and I want to allow the currently logged-in user to be able to declare himself and only himself as a candidate in an election.
I'm using Django's built-in ModelForm and CreateView. My problem is that the Run for Office form (in other words, the 'create candidate' form) allows the user to select any user in the database to make a candidate.
I want the user field in the Run for Office to be automatically set to the currently logged-in user, and for this value to be hidden, so the logged-in user cannot change the value of the field to someone else.
views.py
class CandidateCreateView(CreateView):
model = Candidate
form_class = CandidateForm
template_name = 'candidate_create.html'
def form_valid(self, form):
f = form.save(commit=False)
f.save()
return super(CandidateCreateView, self).form_valid(form)
forms.py
class CandidateForm(forms.ModelForm):
class Meta:
model = Candidate
models.py
class Candidate(models.Model):
user = models.ForeignKey(UserProfile)
office = models.ForeignKey(Office)
election = models.ForeignKey(Election)
description = models.TextField()
def __unicode__(self):
return unicode(self.user)
def get_absolute_url(self):
return reverse('candidate_detail', kwargs={'pk': str(self.id)})
Thanks for reading. I've been stuck on this problem a couple days now, and would really appreciate your help. If you need more info, please lemme know.
Remove user field from rendered form (using
exclude
orfields
, https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#selecting-the-fields-to-use )class CandidateForm(forms.ModelForm): class Meta: model = Candidate exclude = ["user"]
Find user profile and set user field in the create view.
class CandidateCreateView(CreateView): ... def form_valid(self, form): candidate = form.save(commit=False) candidate.user = UserProfile.objects.get(user=self.request.user) # use your own profile here candidate.save() return HttpResponseRedirect(self.get_success_url())
这篇关于在Django中,如何将窗体中的用户字段设置为当前登录的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!