您好,我在我的django应用程序中使用了多个数据库,但是当我尝试调用is_valid()函数或.save()时,django不知道如何使用数据库...我的问题是:如何我可以告诉django表单使用特定的数据库吗?
这是我的代码:
形式:
class ClienteForm(ModelForm):
class Meta:
model = Cliente
def __init__(self, *args, **kwargs):
vUserProfile = kwargs.pop('vUserProfile', None)
super(ClienteForm, self).__init__(*args, **kwargs)
self.fields["idcidade"].queryset = Cidade.objects.using(vUserProfile.dbname).all()
self.fields["idpessoa"].widget.attrs['class'] = "input-mini"
self.fields["idpessoa"].widget.attrs['readonly'] = "True"
这是我的观看代码:
def editaCliente(request, pessoa_id=None):
if request.user.is_authenticated():
profile = request.user.get_profile()
if pessoa_id:
cliente = Cliente.objects.using(profile.dbname).get(idpessoa=pessoa_id)
else:
cliente = None
if request.method == 'POST':
formPessoa = ClienteForm(request.POST, instance=cliente, vUserProfile=profile)
if formPessoa.is_valid():
cliente = formPessoa.save(commit=False)
cliente.idrepresentante = profile.id_comerx3c # passando o id do representante
cliente.internet = 'S'
cliente = formPessoa.save()
if cliente:
return redirect('listaCliente')
else:
formPessoa = ClienteForm(instance=cliente, vUserProfile=profile)
return render_to_response(
'cliente_novo.html',
locals(),
context_instance=RequestContext(request),
)
我尝试使用.is_valid(使用= profile.dbname),但django对我说使用不是参数..
这是TraceBack:
Django Version: 1.4.3
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django_evolution',
'pagination',
'bootstrap_toolkit',
'tastypie',
'comerx')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'pagination.middleware.PaginationMiddleware')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\point\comerx\views.py" in editaCliente
63. if formPessoa.is_valid():
File "C:\Python27\lib\site-packages\django\forms\forms.py" in is_valid
124. return self.is_bound and not bool(self.errors)
File "C:\Python27\lib\site-packages\django\forms\forms.py" in _get_errors
115. self.full_clean()
File "C:\Python27\lib\site-packages\django\forms\forms.py" in full_clean
272. self._post_clean()
File "C:\Python27\lib\site-packages\django\forms\models.py" in _post_clean
338. self.validate_unique()
File "C:\Python27\lib\site-packages\django\forms\models.py" in validate_unique
347. self.instance.validate_unique(exclude=exclude)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in validate_unique
633. errors = self._perform_unique_checks(unique_checks)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in _perform_unique_checks
724. if qs.exists():
File "C:\Python27\lib\site-packages\django\db\models\query.py" in exists
565. return self.query.has_results(using=self.db)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in has_results
441. return bool(compiler.execute_sql(SINGLE))
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
818. cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py" in execute
40. return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django_firebird-1.5.0.alpha.1-py2.7.egg\firebird\base.py" in execute
146. return self.cursor.execute(q, params)
File "C:\Python27\lib\site-packages\fdb\fbcore.py" in execute
3040. PreparedStatement(operation, self, True))
File "C:\Python27\lib\site-packages\fdb\fbcore.py" in __init__
1646. "Error while preparing SQL statement:")
Exception Type: DatabaseError at /comerx/cliente/novo/
Exception Value: ('Error while preparing SQL statement:\n- SQLCODE: -204\n- Dynamic SQL Error\n- SQL error code = -204\n- Table unknown\n- PESSOA\n- At line 1, column 41', u'-204 -- SELECT FIRST 1 (1) AS "A" FROM "PESSOA" WHERE "PESSOA"."IDPESSOA" = -1')
全部谢谢