django is_valid()抛出数据库错误

时间:2012-10-17 07:33:19

标签: django django-forms django-orm

我的is_valid()函数抛出数据库错误。我正在使用别名数据库(不是默认数据库),因此无法验证我的表单。我的Application表位于database2中。  代码:

settings.py

DATABASES = {
      default:{
                 database1
       }
      'database2':{
                  database2
       }
}

models.py

class Application(models.Model)
            application_name = models.CharField(max_length=48,primary_key=True )
            application_version = models.DecimalField(max_digits=3,decimal_places=0,blank=False,null=False)  

forms.py

class Application_form(forms.ModelForm):
    def __init__(self,*args, **kwargs):

        super(Application_form,self).__init__(*args, **kwargs)

class Meta:
    model = Application  

views.py

Application_Form=Application_form(request.POST)    
       if Application_Form.is_valid():
           print "true"
       else:
           print "false"

1 个答案:

答案 0 :(得分:1)

class Application_form(forms.ModelForm):
    def __init__(self,*args, **kwargs):

        super(Application_form,self).__init__(*args, **kwargs)
        self.queryset = Application.objects.using('database2')

    class Meta:
        model = Application  

Link to documentation