I have a problem about querying foreign key field.
All fields I can retrieve except foreign key
// models.py
from django.db import models
from django.contrib.auth.models import User
from users.models import UserProfile
class Video(models.Model):
title = models.TextField()
description = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
// schema.py
class VideoType(DjangoObjectType):
class Meta:
model = Video
md5-dbde95958aa6aab91b8d33a7def1be79
query {
home_videos {
title
description
author
}
}
Following is error message in GraphQLView.
Cannot query field author on type VideoType
The reason author is not showing up is because you don't have an AuthorType defined.
if you only want the id and do not want to define an AuthorType you can probably run the following query
query {
home_videos {
title
description
authorId
}
The real field that django places on the db model is {{model_name}}_id
It then uses magic to place the author object onto your video model at runtime
Graphene Django also does some magic where if you have a foreign key it will look for a DjangoObjectType that refrences the model and automatically map it to the foreignkey relationship.
So if you add this to your schema
class UserType(DjangoObjectType):
class Meta:
model = User
your query will still break but with a different error. You will now need to ask for fields on author. So the following query should work
query {
home_videos {
title
description
author{
id
//other user fields
}
}
}
I'm going to close this issue, please reopen if this doesn't work
Most helpful comment
The reason author is not showing up is because you don't have an AuthorType defined.
if you only want the id and do not want to define an AuthorType you can probably run the following query
The real field that django places on the db model is {{model_name}}_id
It then uses magic to place the author object onto your video model at runtime
Graphene Django also does some magic where if you have a foreign key it will look for a DjangoObjectType that refrences the model and automatically map it to the foreignkey relationship.
So if you add this to your schema
your query will still break but with a different error. You will now need to ask for fields on author. So the following query should work
I'm going to close this issue, please reopen if this doesn't work