GenericRelations
See original GitHub issueI’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:
- Created 7 years ago
- Reactions:6
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m using
GenericRelation
successfully with a simple resolver. Something like this should work: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/
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
schema.py
graphql query: