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.

I’ve been reading through the source code and the docs and I’ve been trying to get a GenericRelation to work, but I can’t work it out. I feel like there’s some cleverness to be done with a resolve method. Any tips?

# ignoring imports

class TaggedItem(models.Model):
    tag = models.SlugField()
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')  # does not resolve

class Bookmark(models.Model):
    url = models.URLField()
    tags = GenericRelation(TaggedItem)  # is not automatically available

class TaggedItemNode(graphene_django.types.DjangoObjectType):
    # parent = graphene.ID() ... ?

    # resolve_parent(...) ... ?

    class Meta:
        model = TaggedItem
        interfaces = (graphene.Node,)

class BookmarkNode(graphene_django.types.DjangoObjectType):
    # tags = graphene.ID()  ... ?

    # resolve_tags(...) ... ?

    class Meta:
        model = Bookmark
        interfaces = (graphene.Node,)

Any help much appreciated.

P

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:6
  • Comments:6

github_iconTop GitHub Comments

8reactions
jameswysecommented, Oct 11, 2016

I’m using GenericRelation successfully with a simple resolver. Something like this should work:

class Bookmark(models.Model):
    url = models.URLField()
    tags = GenericRelation(TaggedItem, related_query_name="bookmarks")

class BookmarkNode(graphene_django.types.DjangoObjectType):
    tags = graphene.relay.ConnectionField(TaggedItemNode)

    def resolve_tags(self, args, context, info):
        return self.tags.all()

    class Meta:
        model = Bookmark
        interfaces = (graphene.Node,)

I don’t use the reverse/parent lookup in my app so I’m not too sure how to do that. The resolver would be simple enough, but you’d need to determine the type dynamically somehow (since content_object could be any model)

It looks like graphene has support for dynamic types; there’s not much documentation but the tests might help: https://github.com/graphql-python/graphene/blob/master/graphene/types/tests/test_typemap.py https://github.com/graphql-python/graphene/blob/master/graphene/types/tests/test_union.py

The Django docs on contenttypes / generic relations are worth reading through too: https://docs.djangoproject.com/en/1.10/ref/contrib/contenttypes/

2reactions
themotucommented, Jul 21, 2019

This ended up being simpler than expected. I feel like this could be automatic in graphene-django but haven’t dug very much.

models.py

class Bookmark(models.Model):
    url = models.URLField()
    tags = GenericRelation(TaggedItem)

schema.py

class TaggedItemType(DjangoObjectType):
    class Meta:
        model = TaggedItem

class Bookmark(DjangoObjectType):
    class Meta:
        model = Bookmark

    tags = graphene.List(TaggedItemType)

    def resolve_tags(self, args):
        return self.tags.all()

graphql query:

{
  bookmark(id:1) {
    id
    url
    tags {
      tag
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use Django's Generic Relations
Basically it's a built in app that keeps track of models from the installed apps of your Django application. And one of the...
Read more >
The contenttypes framework | Django documentation
Relations between your models and ContentType can also be used to enable “generic” relationships between an instance of one of your models and...
Read more >
How Django's generic relations really work? - Medium
In a relational database, a one-to-many relationship is created when a parent record in one table can reference several child records in another...
Read more >
How we used generic relations to add comments to model ...
Here we have the GenericRelations to save us! In a simple way we can say that GenericRelation is a foreign key that can...
Read more >
Where should we use content types and generic relations in ...
Generic Relations are a special type of relation that Django supports where the relationship (foriegn key) is not bound to one particular table....
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