我真的需要设置一个数据库来建模表单用于验证和保存所有数据..我怎样才能传递这种类型的参数?喜欢.using(dbname)在视图中?
不使用db route,因为我需要所有auth数据库并选择进入特定数据库,所有其他数据库设置在我的UserProfile数据库名称中,我尝试使用路由器但不起作用。
我有这个观点:
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),
)
但是当调用formPessoa.is_valid()时显示这些错误:
('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')
这是因为表单没有获得正确的数据库,它采用默认数据库,我只使用auth配置文件。
这是我的模型代码:
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"
由于
答案 0 :(得分:0)
当您在表单上调用save()
时,请传递数据库的名称,如下所示:
cliente = formPessoa.save(using=profile.dbname)