Opened 7 years ago

Last modified 7 weeks ago

#13677 assigned Bug

ModelFormSet may query wrong database backend

Reported by: Pierre Chifflier Owned by: Tobias Kunze
Component: Forms Version: 1.2
Severity: Normal Keywords:
Cc: dgouldin@…, rixx@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When using a ModelFormSet with a queryset using a DB different from the 'default' one, the validation of the fields fails.

debugging the code shows that the db used is the wrong one:

-> self.instance.clean_fields(exclude=exclude)
-> setattr(self, f.attname, f.clean(raw_value, self))
-> self.validate(value, model_instance)
-> qs =**{self.rel.field_name:value})
> /usr/lib/pymodules/python2.5/django/db/models/
-> return self.get_query_set().filter(*args, **kwargs)
(Pdb) p self.get_query_set().db

The ModelFormSet was created by creating Form with a queryset:

def make_projecttask_formset(p, extra=0):
    class _ProjectTaskForm(forms.ModelForm):
        project = forms.ModelChoiceField(label="Project",
    return modelformset_factory(ew_projecttask, form=_ProjectTaskForm, extra=extra)

Expected behavior: used db should be the same as in the queryset

Note: it seems the problems is known (bug not fixed):

Attachments (1)

13677.diff (3.1 KB) - added by David Gouldin 7 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 7 years ago by Russell Keith-Magee

Component: UncategorizedDatabase layer (models, ORM)
milestone: 1.3
Triage Stage: UnreviewedAccepted

comment:2 Changed 7 years ago by David Gouldin

Cc: dgouldin@… added
Owner: changed from nobody to David Gouldin

Changed 7 years ago by David Gouldin

Attachment: 13677.diff added

comment:3 Changed 7 years ago by David Gouldin

Has patch: set

comment:4 Changed 6 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:5 Changed 6 years ago by patchhammer

Easy pickings: unset
Patch needs improvement: set

13677.diff fails to apply cleanly on to trunk

comment:6 Changed 6 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

comment:11 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:12 Changed 22 months ago by Tim Graham

Component: Database layer (models, ORM)Forms
Summary: wrong backend with multidb and modelformsetModelFormSet may query wrong database backend

comment:13 Changed 7 weeks ago by Tobias Kunze

Cc: rixx@… added
Has patch: unset
Patch needs improvement: unset
Resolution: worksforme
Status: newclosed

I tried to build a ModelFormSet that uses the alternative database, and as long as you specify for related objects to use the alternative database, too, the validation is successful. (If you forget to specify the second database, it throws a very explicit error stating that this model relation is not possible).

My test case is avaliable here: - please provide a failing example if you want to reopen this ticket.

comment:14 Changed 7 weeks ago by Tim Graham

Just to be sure, could you clarify what the mistake is in the test in the patch attached to the ticket (13677.diff​)?

comment:15 Changed 7 weeks ago by Tobias Kunze

Has patch: set
Resolution: worksforme
Status: closednew

I'm sorry: I made a mistake in reproducing the issue and porting the patch to current Django. After porting the patch I can confirm that the regression test is valid. I'm not quite sure the implementation is what we want, but I'll push it and we can discuss the PR.

comment:16 Changed 7 weeks ago by Tobias Kunze

Owner: changed from David Gouldin to Tobias Kunze
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top