graphene-django-extras icon indicating copy to clipboard operation
graphene-django-extras copied to clipboard

Get amount of filtered objects returned by DjangoFilterPaginateListField (before pagination)

Open EmiM opened this issue 6 years ago • 7 comments

Hello, is there a direct (clear) way for accessing amount of objects returned by DjangoFilterPaginateListField? I need amount of filtered objects before they are split to pages. Example: if I have 10 objects in db, I filter them by some key and I get 7 objects. I will be paginating this result but I still need the number (7) of objects returned by filter.

Thank you!

EmiM avatar Apr 12 '18 10:04 EmiM

Hi @EmiM - I was just investigating this very thing this week and I ended up with the following solution.

DjangoListObjectField actually does filtering and pagination, and returns totalCount. Ironically, DjangoFilterPaginateListField does not. Not sure why.

class ResourceType(DjangoObjectType):
    class Meta:
        description = "Type definition for resource"
        model = Resource
        filterset_class = ResourceFilter

class ResourceListType(DjangoListObjectType):
    class Meta:
        description = "Type definition for resources uses in list queries"
        model = Resource

class Query(object):
    resource = DjangoObjectField(ResourceType)
    resources = DjangoListObjectField(ResourceListType, filterset_class=ResourceFilter, description=('All Resources query'))

I hope this helps!

atomboulian avatar May 11 '18 22:05 atomboulian

Hello @EmiM and @atomboulian, thanks for asking. Sorry for the delay in responding, but the work has kept me very busy. Could you show me a complete example where DjangoFilterPaginateListField does not return the totalCount field? Although I propose you that try the DjangoSerializerType, since it serves both to define the types and to define the mutations based on a serializer.

eamigo86 avatar May 15 '18 17:05 eamigo86

Hi @eamigo86! Sorry for my delay in responding - also busy at work. Here's what we are experiencing:

screen shot 2018-07-18 at 12 17 30 pm

class Project(AuditTrailModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=255)
    ref_id = models.CharField(max_length=50, blank=True, help_text="The client's personal reference identifier.")
    project_type = models.CharField(max_length=25, choices=PROJECT_TYPE_CHOICES)
    address_line_1 = models.CharField(max_length=255, blank=True)
    address_line_2 = models.CharField(max_length=255, blank=True)
    city = models.CharField(max_length=100, blank=True)
    state_province = models.CharField(max_length=255, blank=True)
    postal_code = models.CharField(max_length=10, blank=True)
    country = CountryField()
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, default=get_current_user, on_delete=models.SET_DEFAULT, related_name="owned_projects")
    target_occupancy = models.DateField()

    def __str__(self):
        return self.name
class ProjectType(DjangoObjectType):
    room_count = graphene.Int()
    planned_spend = graphene.Float()
    budget_variance = graphene.Float()

    class Meta:
        description = "Type definition for project"
        model = Project
        filterset_class = ProjectFilter

class Query(object):
    project = DjangoObjectField(ProjectType)
    projects = DjangoFilterPaginateListField(ProjectType, filterset_class=ProjectFilter, description="Project list query")

atomboulian avatar Jul 18 '18 19:07 atomboulian

Let me know if you'd like more code. Basically, there is no totalCount field alongside the array of ProjectType coming back in the response. Thanks for this amazing library!

atomboulian avatar Jul 18 '18 19:07 atomboulian

Is there any solution?

ldsad7 avatar Sep 12 '19 14:09 ldsad7

any news? is this solved?

digitalw avatar Nov 22 '19 07:11 digitalw

A new release will be released soon, with many improvements and new features.

eamigo86 avatar Nov 22 '19 08:11 eamigo86