[Solved]-Annotate with django-graphene and filters

2👍

For queryset to work you need to get instance of the model which you can get using

queryset = MyModel.objects.annotate(cost_amt=Sum('cost_amt', output_field=FloatField()))

and then you can try further actions.

return MyModelFilter(data=format_query_args(args),queryset=queryset).qs

still error

Try ASSET UNION and see if that works else you can also try DjangoConnectionField from relay.connection.

1👍

It looks like django-graphene is expecting a QuerySet of model instances, when you call .values() on a QuerySet you will get back a queryset of dictionaries, hence the Received incompatible instance.

You may be able to use subquery expressions, or do some rawsql queries both of which will return model instances, it really depends on the type of relationships you have setup on your models.

It almost looks like it may be easier to do this annotation from the other end of the relationship (customer side) as you wouldn’t need the to use .values() but it may not work for what you need.

1👍

I assume that your problem occurs because .values() returns a dictionary rather than a model instance.

You can try to avoid the usage of .values() as you can see in the last two examples of the aggregation cheat sheet and in the Generate aggregates for each item in a QuerySet section:

def my_resolver(self, args, context, info):
    queryset = MyModel.objects.annotate(
        cost_amt=Sum('cost_amt', output_field=FloatField()))

return MyModelFilter(
    data=format_query_args(args),
    queryset=queryset).qs   

But keep in mind that you will have to adjust your “front-end”/receiver accordingly because now your resolver will return a queryset and not a dictionary.

Leave a comment