Django 1.10模型保存问题

时间:2017-02-10 17:00:14

标签: python django modelform

我正在与一些应该简单的事情作斗争。事实上,我之前已经这样做了,而且我似乎无法理解正在发生的事情。

我想做的就是从我的前端保存一个模型......

我在使用Django 1.10

models.py

class Information(forms.ModelForm):
    class Meta:
        model = Fathers
        fields = ('id','first','middle','last','city_of_birth','region_of_birth','country_of_birth',)   

    def __init__(self, *args, **kwargs):        
        self.helper = FormHelper()    
        self.helper.form_class='form-horizontal'
        self.helper.form_id = 'id-information'
        self.helper.form_method = 'post'
        self.helper.layout = Layout(
            Div(
                 FormActions(Submit('dbPostother', 'Save data', css_class='btn btn-success btn-lg', action=".")),
                  css_class = 'col-md-3'
              )
        )
        super(Information, self).__init__(*args,**kwargs)

urls.py

url(r'^dbPostother$',app.views.dbPostother,name='dbPostother'),

views.py

def dbPostother(request):
    if request.method == 'POST':
        form = Information(request.POST)
        if form.is_valid():
            form.save()                                    
    context = {'year':datetime.now().year}
    return render(request, 'app/index.html', context)   

models.py

class Fathers(models.Model):
    id = models.BigIntegerField(primary_key = True,default=1)    
    last = models.TextField(blank=True, null=True)
    first = models.TextField(blank=True, null=True)
    middle = models.TextField(blank=True, null=True)
    city_of_birth = models.ForeignKey(CitiesCity,blank=True, null=True)
    country_of_birth = models.ForeignKey(CitiesCountry,blank=True, null=True)
    region_of_birth = models.TextField(blank=True,null=True)

    def __unicode__(self):
        return self.first +" "+ self.last

    class Meta:
        managed = True
        db_table = 'fathers'
        verbose_name = 'Father'
        verbose_name_plural = 'Fathers'

这应该很简单 - 但数据库没有更新......

由于

1 个答案:

答案 0 :(得分:0)

GAH !!!整天......完全错过了...需要补充:

self.helper.form_action = 'dbPostother'