私は本当にすべてのデータを検証して保存するためにフォームの使用をモデル化するためにデータベースを設定する必要があります..このタイプのパラメーターを渡すにはどうすればよいですか?ビューで.using(dbname)のように?
すべての認証データベースが必要であり、特定のデータベースにアクセスすることを選択したため、db routeを使用せずに、他のすべてがUserProfileデータベース名に設定され、IIはルーターを使用しようとしましたが、機能しません。
私はこの見解を持っています:
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')
これは、フォームが正しいデータベースを取得しないため、デフォルトのデータベースを使用します。これは、認証プロファイルにのみ使用しています。
これが私のモデルフォームコードです:
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"
ありがとう