I have a form in my application which has a hidden form field, the value of which I want to set in my corresponding view after submitting the form.
我的应用程序中有一个表单,其中有一个隐藏的表单字段,我想在提交表单后在相应的视图中设置其值。
forms.py
forms.py
class EvangelizedForm(forms.ModelForm):
first_name = forms.CharField(help_text="First Name")
last_name = forms.CharField(help_text="Last Name")
email = forms.CharField(help_text="Email ID")
mobile_no = forms.CharField(help_text="Mobile number")
twitter_url = forms.CharField(help_text="Twitter URL")
twitter_followers = forms.CharField(widget = forms.HiddenInput()) #Hidden form field
class Meta:
model = Evangelized
fields = ('first_name','last_name', 'twitter_url', 'email', 'mobile_no')
models.py
models.py
class Evangelized(models.Model):
first_name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
email = models.EmailField()
mobile_no = models.CharField(unique=True, max_length = 10, validators=[RegexValidator(regex='^\w{10}$', message='Mobile number should be strictly of 10 digits.')])
twitter_url = models.CharField(unique=True, max_length=128)
twitter_followers = models.CharField(max_length = 128)
views.py
views.py
def fillform(request):
follower_count = '250'
if request.method == 'POST':
form = EvangelizedForm(request.POST)
if form.is_valid():
form.fields['twitter_followers'] = follower_count
form.save(commit=True)
return index(request)
else:
form.errors
else:
#form = EvangelizedForm()
if request.user.is_authenticated():
form = EvangelizedForm(initial={'first_name': request.user.first_name,
'twitter_url': 'https://twitter.com/' + request.user.username,
'last_name': request.user.last_name})
else:
form = EvangelizedForm()
context = RequestContext(request,
{'request': request,
'user': request.user, 'form':form})
#return render(request, 'rango/fillform.html', {'form': form, 'context_instance':context})
return render_to_response('rango/fillform.html',
context_instance=context)
Basically, I'm trying to set the value of twitter_followers
(which is a hidden form field in forms.py) in my index
view, by:
基本上,我试图在我的索引视图中设置twitter_followers(在forms.py中是一个隐藏的表单字段)的值,方法是:
follower_count = '250'
..
..
form.fields['twitter_followers'] = follower_count
By doing this, I'm expecting the value of 'twitter_followers'
in the database after submitting the form to be '250'
. However, this approach doesn't seem to be working.
通过这样做,我希望在将表单提交为'250'后,数据库中的'twitter_followers'值。但是,这种方法似乎不起作用。
What's the right way to set values to certain attributes in the database manually using views?
使用视图手动为数据库中的某些属性设置值的正确方法是什么?
2 个解决方案
#1
1
You need to set it on the model instance, which is the result of form.save
. That's the main reason for the commit
argument in the first place.
您需要在模型实例上设置它,这是form.save的结果。这是首先提交参数的主要原因。
if form.is_valid()
obj = form.save(commit=True)
obj.twitter_follower = follower_count
obj.save()
#2
0
You can override the save method of the form, with something like this:
您可以使用以下内容覆盖表单的save方法:
def save(self, *args, **kwargs)
twitter_followers = kwargs.pop('twitter_followers', 0)
self.instance.twitter_followers = twitter_followers
super(Evangelized, self).save(args, kwargs)
And then in the view just have to call in this way:
然后在视图中只需要以这种方式调用:
form.save(twitter_followers=250)
#1
1
You need to set it on the model instance, which is the result of form.save
. That's the main reason for the commit
argument in the first place.
您需要在模型实例上设置它,这是form.save的结果。这是首先提交参数的主要原因。
if form.is_valid()
obj = form.save(commit=True)
obj.twitter_follower = follower_count
obj.save()
#2
0
You can override the save method of the form, with something like this:
您可以使用以下内容覆盖表单的save方法:
def save(self, *args, **kwargs)
twitter_followers = kwargs.pop('twitter_followers', 0)
self.instance.twitter_followers = twitter_followers
super(Evangelized, self).save(args, kwargs)
And then in the view just have to call in this way:
然后在视图中只需要以这种方式调用:
form.save(twitter_followers=250)