Django:使用视图设置数据库中某些属性的值?

时间:2015-07-05 00:20:54

标签: python django python-2.7 django-models django-forms

我的应用程序中有一个表单,其中有一个隐藏的表单字段,我希望在提交表单后在相应的视图中设置该字段。

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

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

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)

基本上,我试图在twitter_followers视图中设置index(在forms.py中隐藏的表单字段)的值,通过:

follower_count = '250'
..
..
form.fields['twitter_followers'] = follower_count

通过这样做,我在提交表单'twitter_followers'后期望数据库中'250'的值。但是,这种方法似乎并没有起作用。

使用视图手动为数据库中的某些属性设置值的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

您需要在模型实例上设置它,这是form.save的结果。这是commit论证的主要原因。

if form.is_valid()
    obj = form.save(commit=True)
    obj.twitter_follower = follower_count
    obj.save()

答案 1 :(得分:0)

您可以使用以下内容覆盖表单的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)

然后在视图中只需要以这种方式调用:

form.save(twitter_followers=250)