[melbourne-pug] Specify the database for a Django ModelForm instance

Anthony Briggs anthony.briggs at gmail.com
Mon Aug 8 21:12:00 EDT 2016


Hi Ben,

The database is normally routed via the model, rather than the form, so a
ModelForm would generally just pick whatever the model uses. I would
imagine that trying to hack on the form directly would be a Bad Plan(tm).

https://docs.djangoproject.com/en/1.10/topics/db/multi-db/#automatic-database-routing

That has info on setting up simple read/write replicas, 'using' in raw
queries, etc. too.

Hope that helps,

Anthony


On 9 August 2016 at 10:38, Ben Finney <ben+python at benfinney.id.au> wrote:

> Howdy all,
>
> How can I specify which database (by its alias name) a Django ModelForm
> should use?
>
> (I'm having trouble getting the message onto the Django forum, so I'm
> asking this Python-related question here too.)
>
> A Django ModelForm knows its corresponding model, and the fields
> included.
>
> The ModelForm instance clearly knows how to specify a database,
> internally. It can validate its fields against the database, and can
> save a new model instance to the database. This implies its operations
> have knowledge of which database to use.
>
> What I need is to access that as an external user, when creating the
> instance. I can't find how to specify any database other than the
> default, when creating the ModelForm nor when it interacts with the
> database.
>
> What is the equivalent for using='foo' when instantiating a ModelForm
> for the model, or calling its methods (ModelForm.clean, ModelForm.save,
> etc.)?
>
> --
>  \        “A free press is one where it's okay to state the conclusion |
>   `\                      you're led to by the evidence.” —Bill Moyers |
> _o__)                                                                  |
> Ben Finney
>
> _______________________________________________
> melbourne-pug mailing list
> melbourne-pug at python.org
> https://mail.python.org/mailman/listinfo/melbourne-pug
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20160809/c935e936/attachment.html>


More information about the melbourne-pug mailing list