我想按user.username过滤通知,该怎么办?
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)
答案 0 :(得分:2)
您可以使用库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