如何使用石墨烯过滤



我想按用户名过滤通知,我该怎么做?

models.py

class Notification(BaseModelo):
user = models.ForeignKey(User, on_delete=models.CASCADE)
text = models.CharField(max_length=200)
state = models.BooleanField(default=False)

schema.py

class NotificationNode(DjangoObjectType):
class Meta:
model = Notification
filter_fields = ['user']
interfaces = (Node, )
class Query(ObjectType):
user = Node.Field(UserNode))
all_users = DjangoConnectionField(UserNode)
notification = Node.Field(NotificationNode)
all_notifications = DjangoFilterConnectionField(NotificationNode)

你可以使用库django-filter和标准的 Django 双下划线语法来使用相关模型的属性。也就是说,您应该在过滤器字段中写"user__username"。

class NotificationNode(DjangoObjectType):
class Meta:
model = Notification
filter_fields = { 'user__username': ['exact'], }
interfaces = (Node, )

您可以在此处看到使用的示例:https://docs.graphene-python.org/projects/django/en/latest/tutorial-relay/#schema

最新更新