Hi Ben,

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?

FWIW, there's only one instance of 'using' in Model forms, when saving over
an existing instance:
https://github.com/django/django/blob/master/django/forms/models.py#L819 Other
than that, model forms would be using whatever's set for the model.

Worst case, you can create a new subclass, override the ModelForm's save()
method (
https://github.com/django/django/blob/master/django/forms/models.py#L433)
and make it do what you need, but that overrides a bunch of Django's
guarantees (mainly that the model table will be there to save to, or that
it'll be in sync with what you currently have in memory)

Anthony


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

> Anthony Briggs <[email protected]> writes:
>
> > The database is normally routed via the model, rather than the form,
> > so a ModelForm would generally just pick whatever the model uses.
>
> Okay, so how do I specify the arguments the ModelForm will use when
> interacting with the Model instance it creates?
>
> I'm trying to hook into the ModelForm's validation (ModelForm.clean) and
> model creation (ModelForm.save) behaviour, to tell it which database to
> use for that operation.
>
> > I would imagine that trying to hack on the form directly would be a
> > Bad Plan(tm).
>
> Sure, I'm trying to do this as an external user of the ModelForm.
>
> > https://docs.djangoproject.com/en/1.10/topics/db/multi-
> db/#automatic-database-routing
>
> I'm not interested in automatically routing all operations; I want to
> *specify* which database, for a particular operation.
>
> If it matters: this is in a management command, where I need to be able
> to specify from the command line that a particular database alias is the
> context for a command.
>
> The same way I can with the ‘using’ hook of ModelManager.using('foo'),
> or Model.save(using='foo'). What is the equivalent for a ModelForm
> instance?
>
> --
>  \         Fry: “Take that, poor people!”  Leela: “But Fry, you’re not |
>   `\     rich.”  Fry: “No, but I will be someday, and then people like |
> _o__)                                  me better watch out!” —Futurama |
> 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