1

我想按 user.username 过滤通知,我该怎么做?

模型.py

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

架构.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)
4

1 回答 1

3

您可以使用库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

于 2019-12-22T12:07:44.120 回答