Opened 13 years ago

Closed 2 years ago

#15099 closed New feature (wontfix)

ModelFormset.queryset requirement is too strict

Reported by: Jari Pennanen Owned by: nobody
Component: Forms Version: 1.2
Severity: Normal Keywords:
Cc: dtrebbien Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Why is the ModelFormset requiring QuerySet? With very few modifications to BaseModelFormset it could be sequence, list or RawQuerySet.

In my specific use case I needed to use RawQuerySet in the ModelFormset, following workaround works for RawQuerySets and lists of objects:

def edit_users(request):
    class FakeQuerySet(list):
        def __init__(self, seq):
            super(FakeQuerySet, self).__init__(seq)
            self.ordered = True
            self.db = hasattr(seq, 'db') and seq.db or seq[0]._state.db

    users = FakeQuerySet(User.objects.raw('SELECT * FROM auth_user')) # Test with RawQuerySet
    #users = FakeQuerySet(list(User.objects.raw('SELECT * FROM auth_user'))) # Test with list

    UserFormset = modelformset_factory(User, extra=3)
    userforms = UserFormset(data=request.POST or None, queryset=users)
    ...

Change History (4)

comment:1 by Russell Keith-Magee, 13 years ago

Component: UncategorizedForms
Triage Stage: UnreviewedAccepted

On principle, no objection here. Of course, the devil is in the detail -- especially around the implicit preconditions that querysets satisfy that a list may not necessarily satisfy (such as guaranteed ordering).

This would also be a good exercise in abstraction, separating the "list-like" bits of formsets from the queryset-specific bits.

comment:2 by James Addison, 13 years ago

Severity: Normal
Type: Bug

comment:3 by dtrebbien, 13 years ago

Cc: dtrebbien added
Easy pickings: unset
UI/UX: unset

comment:4 by Mariusz Felisiak, 2 years ago

Resolution: wontfix
Status: newclosed
Type: BugNew feature

This is really niche and probably it's not worth additional complexity. We can reconsider this decision only if someone provides PoC.

Note: See TracTickets for help on using tickets.
Back to Top