石墨烯Django过滤器的一般关系

时间:2019-05-22 11:17:20

标签: python django graphql graphene-python

我正在将graphene-django与django-filter一起使用。用django的GenericForeignKey过滤对象时出现问题。我尝试了自定义FilterSet,但没有运气。这是我的模型和架​​构。

class Feature(models.Model):
    start_time = models.DateTimeField(db_index=True, validators=[validate_start_time])
    content_type = models.ForeignKey(
        ContentType, on_delete=models.CASCADE, limit_choices_to={"model__in": ["album", "collection", "mix"]}
    )
    object_id = models.UUIDField(db_index=True, unique_for_date="start_time")
    item = GenericForeignKey("content_type", "object_id")

    objects = FeatureQuerySet.as_manager()

class Album(models.Model):
    cover = ImageField(upload_to="album", blank=True, help_text="600x600")
    single = models.BooleanField(default=False)
    featured = GenericRelation("Feature")

这是我使用DjangoObjectType的架构:

class FeatureNode(DjangoObjectType):
    class Meta:
        model = Feature
        interfaces = (relay.Node,)
        filter_fields = ("content_type",)

class AlbumNode(DjangoObjectType):
    class Meta:
        model = Album
        filter_fields = ("is_new_release", "single", "featured")
        interfaces = (relay.Node,)

class FeatureFilter(django_filters.FilterSet):
    featured__start_time = django_filters.DateFilter()

    class Meta:
        model = Album
        fields = ("featured",)

class Query(ObjectType):
    album = relay.Node.Field(AlbumNode)
    albums = DjangoFilterConnectionField(AlbumNode, filterset_class=FeatureFilter)

我想做的是我想获得带有Albums标志的featured:True查询。如下所示:

query FeaturedAlbums {
   albums(featured:True) {
     edges {
       node {
         id
       }
     }
   }
}

0 个答案:

没有答案