0

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?

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.

I have this view:

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),
    )

but when call formPessoa.is_valid() show me these error:

('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')

Thats because the form doesn't get the correct database, it`s take a default database, what I'm use only for auth profiles.

Here is my modelform 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"

Thanks

fh_bash
  • 1,725
  • 4
  • 16
  • 23
  • post some code of what you have done so far. plus explain more. one-liner questions are not helpful... – Srikar Appalaraju Jan 15 '13 at 16:25
  • @Srikar Ok, I put some code there, and try to explain better. – fh_bash Jan 15 '13 at 16:28
  • Please can you clarify the following: "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." - am I correct in thinking that you have 2 databases, one which contains all the auth data and one which contains all the other data, including the UserProfile? If this is the case, be aware that [Django does not support cross-database foreign key relations](https://docs.djangoproject.com/en/1.3/topics/db/multi-db/#cross-database-relations), which you'd need for the.. – m01 Jan 15 '13 at 16:45
  • ..user profiles. I think database routers would be the way to go, subject to the constraint I mentioned, which I think you will hit regardless of whether or not you use database routers or `.using`. Now having said that, there is [a question](http://stackoverflow.com/questions/5493241) out there that looks for workarounds.. – m01 Jan 15 '13 at 16:45
  • @m01 Yes.. its correct.. I have the "default" database, only for login and UserProfile, in this UserProfile I have one field named "DBNAME" with I put the name of database the user is connect.. It`s works fine, when I use .using() in a view.. but in a Form, doesn't work.. Other way is to use, is to use router, but I try to use like in this link https://docs.djangoproject.com/en/1.4/topics/db/multi-db/ but doesn't work, it's appears the django doesn't select correct database. – fh_bash Jan 15 '13 at 16:50

1 Answers1

0

When you call save() on your form, pass in the name of the database like so:

cliente = formPessoa.save(using=profile.dbname)
girasquid
  • 15,121
  • 2
  • 48
  • 58
  • Ok, but like I said, the error appears in this line: formPessoa.is_valid(), I try to put formPessoa.is_valid(using=profile.dbname) but django tell me using is not a parameter. – fh_bash Jan 15 '13 at 17:08
  • But even I remove is_valid line, putting save tike you said, I got this error: save() got an unexpected keyword argument 'using' – fh_bash Jan 15 '13 at 17:17
  • My mistake - you want to call it on the model instance you get from the form - see http://stackoverflow.com/q/6118884/240418. – girasquid Jan 15 '13 at 17:19
  • Same error. in line formPessoa.save(commit=False): DatabaseError at /comerx/cliente/novo/ ('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') – fh_bash Jan 15 '13 at 17:31
  • I am not sure what's going on here - calling save with `commit=False` on the form shouldn't be issuing any database queries. Sorry I can't help more :( – girasquid Jan 15 '13 at 20:36