Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set a specific database to use in one ModelForm in Django?
    primarykey
    data
    text
    <p>I'm realy need to set a database to model form use to validate and save all data.. how can I pass this type of parameter? like .using(dbname) in view?</p> <p>Without use db route, because I need all auth database and select to get in a specific database all other is set in my UserProfile database name, and I I try to use router but doesn't work.</p> <p>I have this view:</p> <pre><code>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), ) </code></pre> <p>but when call formPessoa.is_valid() show me these error:</p> <pre><code>('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') </code></pre> <p>Thats because the form doesn't get the correct database, it`s take a default database, what I'm use only for auth profiles.</p> <p>Here is my modelform code:</p> <pre><code>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" </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload