I have read about how to exclude (hide) certain fields in django and graphene_django in these Links:
Imagine that we have the following Post
model which has foreign key to the User
model.
apps/posts/models.py
from django.db import models
from apps.users.models import User
class Post(models.Model):
author = models.ForeignKey(
User,
on_delete=models.CASCADE,
null=False
)
created_at = models.DateTimeField(
auto_now_add=True,
)
title = models.TextField(
null=False,
max_length=100,
)
content = models.TextField(
null=False,
)
def __str__(self):
return self.title
apps/users/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser, models.Model):
phone_no = models.CharField(
blank=True,
null=True,
default="",
max_length=10,
verbose_name="Phone Number",
)
avatar = models.ImageField(
null=True,
upload_to='static',
)
USERNAME_FIELD = "username"
EMAIL_FIELD = "email"
def __str__(self):
return self.username
I tried the following but it doesn't work as expected:
apps/posts/schema.py
import graphene
from graphene import Mutation, InputObjectType, ObjectType
from graphene_django.types import DjangoObjectType
from .models import Post
class PostType(DjangoObjectType):
class Meta:
model = Post
exclude_fields = [
'created_at', #it worked
'author.password', #the way I tried to hide the foreign key field
]
class Query(ObjectType):
posts = graphene.List(
PostType
)
def resolve_posts(self, info):
#TODO: pagination
return Post.objects.all()
Screenshot:
How can I hide certain fields of it (like author's password in the above example) in graphql model Types?
from How to hide/exclude certain fields of foreign key in graphene_django wrt the requested entity?
No comments:
Post a Comment