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

Reply via email to