question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DjangoConnectionField discards select_related

See original GitHub issue

graphene-django==1.3

https://github.com/graphql-python/graphene-django/blob/master/graphene_django/fields.py#L59

When you do this:

# DjangoFilterConnection

    @classmethod
    def merge_querysets(cls, default_queryset, queryset):
        return default_queryset & queryset

you discard any select_related or prefetch_related or annotate that might have been applied to the query set.

When we write a def resolve_things(self,... method and return a customized QuerySet it is then merged into the default_queryset and this discards everything (I think) except the filtering.


queryset = Deals.objects.select_related('client')

> /usr/local/lib/python2.7/dist-packages/graphene_django/fields.py(58)merge_querysets()
     56             return self.model._default_manager
     57
---> 58     @classmethod
     59     def merge_querysets(cls, default_queryset, queryset):
     60         return default_queryset & queryset

ipdb> queryset[0].client
[0.051] SELECT "clients_deal"."id",
       "clients_deal"."client_id",
       "clients_deal"."agent_id",
       "clients_deal"."type",
       "clients_deal"."open",
       "clients_deal"."stage",
       "clients_deal"."created_on",
       "clients_deal"."modified_on",
       "clients_deal"."client_last_active_on",
       "clients_client"."id",
       "clients_client"."contact_id",
       "clients_client"."email",
       "clients_client"."fname",
       "clients_client"."lname",
       "clients_client"."phone",
       "clients_client"."work",
       "clients_client"."verified",
       "clients_client"."suggested_listings",
       "clients_client"."referral_source_id",
       "clients_client"."created_on",
       "clients_client"."modified_on",
       "clients_client"."created_by_id"
FROM "clients_deal"
INNER JOIN "clients_client" ON ("clients_deal"."client_id" = "clients_client"."id")
WHERE ("clients_deal"."agent_id" = 769
       AND "clients_deal"."open" = true
       AND "clients_deal"."agent_id" = 769)
ORDER BY "clients_deal"."client_last_active_on" DESC
LIMIT 1

# no extra query required
<Client: BOb Hop>

# here is after merging using &
ipdb> merged = default_queryset & queryset
ipdb> merged[0].client
[0.006] SELECT "clients_deal"."id",
       "clients_deal"."client_id",
       "clients_deal"."agent_id",
       "clients_deal"."type",
       "clients_deal"."open",
       "clients_deal"."stage",
       "clients_deal"."created_on",
       "clients_deal"."modified_on",
       "clients_deal"."client_last_active_on"
FROM "clients_deal"
WHERE ("clients_deal"."agent_id" = 769
       AND "clients_deal"."open" = true
       AND "clients_deal"."agent_id" = 769)
ORDER BY "clients_deal"."client_last_active_on" DESC
LIMIT 1

[0.006] SELECT "clients_client"."id",
       "clients_client"."contact_id",
       "clients_client"."email",
       "clients_client"."fname",
       "clients_client"."lname",
       "clients_client"."phone",
       "clients_client"."work",
       "clients_client"."verified",
       "clients_client"."suggested_listings",
       "clients_client"."referral_source_id",
       "clients_client"."created_on",
       "clients_client"."modified_on",
       "clients_client"."created_by_id"
FROM "clients_client"
WHERE "clients_client"."id" = 188672

I’m not quite sure what the purpose of merging it with the default query is.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
crucialfelixcommented, Jul 20, 2017
from graphene_django.fields import DjangoConnectionField as _DjangoConnectionField

class DjangoConnectionField(_DjangoConnectionField):

    """
    Temporary fix for select_related issue
    """

    @classmethod
    def merge_querysets(cls, default_queryset, queryset):
        """
        This discarded all select_related and prefetch_related:
        # return default_queryset & queryset
        """
        return queryset
0reactions
crucialfelixcommented, Aug 8, 2017

This bug also affects all auto-generated resolvers for ManyToMany fields

Given:

class Apt(models.Model)
    neighborhoods = models.ManyToManyField("Neighborhood")
class Apt(DjangoObjectType):

    class Meta:
        model = models.Apt

No custom fields or resolve_neighborhoods defined.

query {
  apt(pk: 1) {
    neighborhoods {
      edges { node { id name }}
  }
}

The default resolver returns the apt.neighborhoods.all() but then tries to merge it into Apt.objects.all()

    return Promise.resolve(next(*a, **b))
  File "/usr/local/lib/python2.7/dist-packages/graphene_django/fields.py", line 93, in connection_resolver
    iterable = cls.merge_querysets(default_queryset, iterable)
  File "/usr/local/lib/python2.7/dist-packages/graphene_django/fields.py", line 60, in merge_querysets
    return default_queryset & queryset
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 307, in __and__
    combined.query.combine(other.query, sql.AND)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 513, in combine
    "Cannot combine queries on two different base models."
AssertionError: Cannot combine queries on two different base models.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Selecting specific fields using select_related in Django
I want to select only blog name while extracting the article. articles = Articles.objects.all().select_related('blog__name'). The query ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found