ModelFormset.queryset requirement is too strict
|Reported by:||Ciantic||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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._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 (3)
comment:1 Changed 3 years ago by russellm
- Component changed from Uncategorized to Forms
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted