Before you get this by solved
I tried :
def get_queryset(self, request):
LIMIT_SEARCH = 25
qs = MyObject.objects.filter(medic=request.user).order_by('-id')
return qs
and this:
def get_queryset(self, request):
LIMIT_SEARCH = 25
qs = super(ModelInline, self).get_queryset(request).filter(medic=request.user).order_by('-id')
return qs
That works, but when a add the slice [:LIMIT_SEARCH] did not work for me:
This
MyObject.objects.filter(medic=request.user).order_by('-id')[:LIMIT_SEARCH]
or this:
super(ModelInline, self).get_queryset(request).filter(medic=request.user).order_by('-id')[:LIMIT_SEARCH]
I receive this error
Cannot filter a query once a slice has been taken. django queryset
also max_num doesnt work, because is the query set not an action of user
Thanks for your time;
ANother solution is activate paginator, it could be activate on django jet?
@xtornasol512 Did you try it?
Sure, I give that error when use my LIMIT_SEARCH, just works if I use it without LIMIT_SEARCH
Need this a PR?
I don't know if It is a problem of django or django jet actually. BTW I use the last version of django 1.8
The problem is with these variable.
Hey I could solved this problem!
class MyModelInline(admin.TabularInline):
...
def get_queryset(self, request):
LIMIT_SEARCH = 25
queryset = super(MyModelInline, self).get_queryset(request)
ids = queryset.order_by('-id').values('pk')[:LIMIT_SEARCH]
qs = MyModel.objects.filter(pk__in=ids).order_by('-id')
return qs
Thanks to some guys, but also I would like to do this with paginator instead of limit the query
but that means another issue :P Thanks for his patients Hope this can help someone else
Sorry I found out little bugs with that solution
Sometimes doesnt work with some instances
So the best way to do that was create a formset and override the init method like this
from django.forms import BaseInlineFormSet
class LimitModelFormset(BaseInlineFormSet):
''' Base Inline formset to limit inline Model'''
def __init__(self, *args, **kwargs):
super(LimitModelFormset, self).__init__(*args, **kwargs)
instance = kwargs["instance"]
self.queryset = MyModel.objects.filter(field_instance=instance).order_by('-id')[:25]
class MyModelInline(admin.TabularInline):
...
formset = LimitModelFormset
That was all Thanks !
@xtornasol512 Can you explain us what happen with paginator?
I think I will check the paginator later on weekend so now limit the queryset is perfect to me!
Ok, we will wait for it.
Good solution @xtornasol512 . I've made it a bit more generic so you can include it in any inline.
class LimitModelFormset(BaseInlineFormSet):
""" Base Inline formset to limit inline Model query results. """
def __init__(self, *args, **kwargs):
super(LimitModelFormset, self).__init__(*args, **kwargs)
_kwargs = {self.fk.name: kwargs['instance']}
self.queryset = kwargs['queryset'].filter(**_kwargs).order_by('-id')[:300]
UPDATED: Found it wasn't filtering properly before.
can you make a PR to including this in django-jet?
Most helpful comment
Good solution @xtornasol512 . I've made it a bit more generic so you can include it in any inline.
UPDATED: Found it wasn't filtering properly before.