ModelFormSet validation fails when initial queryset contains objects not in the default manager's queryset

26 views
Skip to first unread message

Tom

unread,
Feb 21, 2012, 12:58:30 AM2/21/12
to django-d...@googlegroups.com
Hi everyone,

I have a ModelForm and a ModelFormSet. I initialize my ModelFormSet with a queryset containing objects from a custom manager that is not the model's default manager. My formset does not validate, because ModelForms with instances that are objects in my queryset but not in the model's default manager's queryset don't have valid ids. 

My code: http://dpaste.org/d6V9y/

I think I'm seeing this because the add_fields method of BaseModelFormSet has the line (659) qs = self.model._default_manager.get_query_set().
I can get my code to work by subclassing BaseModelFormset and setting form.fields[self._pk_field.name].queryset= self.get_queryset() (thanks to some help in irc).
My question is why the default manager's queryset is used instead of self.get_queryset().

Thanks,

Tom

Carl Meyer

unread,
Feb 21, 2012, 10:42:41 AM2/21/12
to django-d...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Tom,

On initial inspection I'd say you're right; it seems like
self.get_queryset() should be used there rather than assuming the
default manager's queryset. Can't be sure until I look into it more
closely - the next step would be to file a ticket at
code.djangoproject.com, ideally with a patch containing a failing test
case integrated into Django's own test suite.

Thanks for the report!

Carl
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9Du3EACgkQ8W4rlRKtE2cPdwCeLId9F+VVVVFYEPGvuq6vpp+I
Pn4AoMUeSvU8msQ22lGgneeUA+/g58b7
=Tv81
-----END PGP SIGNATURE-----

Ramiro Morales

unread,
Feb 21, 2012, 10:05:52 AM2/21/12
to django-d...@googlegroups.com
On Tue, Feb 21, 2012 at 2:58 AM, Tom <lar...@gmail.com> wrote:
> Hi everyone,

Please start threads like this to the django-users mailing list
as this list is dedicated exclusively to discussions about
development of Django.

--
Ramiro Morales

Carl Meyer

unread,
Feb 21, 2012, 10:46:17 AM2/21/12
to django-d...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Ramiro,

It seems to me you may have mis-read Tom's message. It seems perfectly
on-topic to me; he's not asking how to fix his problem, he's asking
about the design of BaseModelFormSet itself.

Carl
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9DvEkACgkQ8W4rlRKtE2eofgCeMgBwlfF3u4KaZLexAex/jdPZ
VK8An33E/ENnb0tHeCytk4qLoBN3/eUe
=P5Fo
-----END PGP SIGNATURE-----

Ramiro Morales

unread,
Feb 21, 2012, 10:43:46 AM2/21/12
to django-d...@googlegroups.com
On Tue, Feb 21, 2012 at 12:46 PM, Carl Meyer <ca...@oddbird.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi Ramiro,
>
> [...]

>
> It seems to me you may have mis-read Tom's message. It seems perfectly
> on-topic to me; he's not asking how to fix his problem, he's asking
> about the design of BaseModelFormSet itself.

Oh you are right. Sorry Tom for replying too fast and thanks Carl for
correting me.

Regards,

--
Ramiro Morales

Reply all
Reply to author
Forward
0 new messages