0

我真的需要设置一个数据库来模拟表单使用来验证和保存所有数据。我怎样才能传递这种类型的参数?像 .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')

那是因为表单没有得到正确的数据库,它需要一个默认数据库,我只用于身份验证配置文件。

这是我的模型代码:

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"

谢谢

4

1 回答 1

0

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

cliente = formPessoa.save(using=profile.dbname)
于 2013-01-15T17:01:38.047 回答