Right, so you can override the ModelForm's save() method, but you lose all
of the other nice Django things, like model init, checking, etc. which
you'll also have to recreate manually.

Or you can do it the Right Way(tm) and write a custom database router as
per the reply before that. Look for `PrimaryReplicaRouter` in
https://docs.djangoproject.com/en/1.10/topics/db/multi-
db/#automatic-database-routing, I'm pretty sure you can make it do what you
need to (eg. with a method/attribute to specify the database name)

Anthony


On 9 August 2016 at 12:35, Ben Finney <[email protected]> wrote:

> Anthony Briggs <[email protected]> writes:
>
> > I'm not sure what you're trying to do. Trying to write a management
> > command using model forms makes no sense - wouldn't you just load the
> > model instance directly in that case?
>
> The ModelForm validation functionality is being used, to validate input.
>
> Pseudocode::
>
>     import csv
>
>     from cumquat_app.forms import CumquatImportForm
>
>     db_alias = 'foo'
>
>     reader = csv.DictReader(input_file)
>     for row in reader:
>         fields = make_fields_from_input_row(reader)
>
>         # Wanted: ‘form = CumquatInputForm(fields, using=db_alias)’.
>         form = CumquatImportForm(fields)
>
>         # Wanted: ‘if form.is_valid(using=db_alias)’.
>         if form.is_valid():
>
>             # Wanted: ‘form.save(using=db_alias)’.
>             form.save()
>
> At both points of interaction with the database – validating the fields,
> and saving an instance – I am expecting a way to specify *which*
> database to interact with.
>
> I have the option to tell the CumquatImportForm (which is a ModelForm
> subclass) to avoid the database when it saves; it returns an instance,
> which I can then instruct to save to a specific database::
>
>                 cumquat = form.save(commit=False)
>                 cumquat.save(using='foo')
>
> But I can't seem to get an equivalent hook for the ‘clean’ or ‘is_valid’
> methods, which also interact with the database to validate field (e.g.
> for unique constraints).
>
> How can I tell the ModelForm that its interactions with the database, be
> they instance creation or querysets or anything else, should be to the
> database whose alias I specify? The equivalent of the ‘using='foo'’, above?
>
> --
>  \     “… a voice said reassuringly: cheer up, things could get worse. |
>   `\    So I cheered up and, sure enough, things got worse.” —James C. |
> _o__)                                               Hagerty, 1909–1981 |
> Ben Finney
>
> _______________________________________________
> melbourne-pug mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/melbourne-pug
>
_______________________________________________
melbourne-pug mailing list
[email protected]
https://mail.python.org/mailman/listinfo/melbourne-pug

Reply via email to