#Getting ModelIterable with values() or alternatives to apply group-by correctly for Graphene

5 messages · Page 1 of 1 (latest)

proud delta
#

Hey Folks! I'm using Graphene Django and trying to curate some very specific queries with ORM. I need the ability to specify a set of existing fields from a Model by which group-by is to be applied, and apply annotations to get some aggregate value.

So if the input is field_1, field_2, count, then I need the query to be SELECT field_1, field_2, count(true) FROM model GROUP BY field_1, field_2. I'm able to achieve this partly with values(). However, values() return a set of dictionary objects, and values_list() return tuples, both of which are not accepted by Graphene. I tried using only() but that adds the primary key as well into the grouping.

I was able to get this working with _values() which I'd prefer not to use since its a private method, but if there are no other options, it would be fine. But then I realized that the columns being fetched are in the wrong order somehow. For example, when I print result.field_1, it prints the value of field_2, and vice-versa. This obviously doesn't happen with values() when I do results.get("field_1").

I was wondering if anyone else has encountered this same issue or have suggestions on how I can:

  1. avoid using _values() and/or
  2. get ModelIterable results but be able to fetch the values for each field correctly.

Versions:
Django: 4.2.5
Graphene-Django: 3.1.5

Database: Snowflake

burnt flint
#

Maybe rewrap the the queryset in an object that rehydrates a model instance?

#

So you can use values

proud delta
#

Thanks CodenameTim. I was able to fix it by passing field names in the order they are defined in the model. Weird why it still happens.

burnt flint