graphene-django-extras
graphene-django-extras copied to clipboard
Get amount of filtered objects returned by DjangoFilterPaginateListField (before pagination)
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!
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!
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.
Hi @eamigo86! Sorry for my delay in responding - also busy at work. Here's what we are experiencing:
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")
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!
Is there any solution?
any news? is this solved?
A new release will be released soon, with many improvements and new features.