0

我的 is_valid() 函数引发数据库错误。我正在使用别名数据库(不是默认数据库),因此无法验证我的表单。我的应用程序表在 database2 中。代码:

设置.py

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

模型.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)  

表格.py

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

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

class Meta:
    model = Application  

视图.py

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

1 回答 1

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  

链接到文档

于 2012-10-17T07:43:56.757 回答