【发布时间】:2019-03-03 14:12:30
【问题描述】:
我正在尝试根据我的 ProductLandingpageImage 模型上的“订单”字段重新排序我的 ProductLandingPageImageNode。
如果这将是一个直接查询,我可以编写一个解析方法,但我无法找出如何在子查询中实现这一点。
主要查询:
class Query(graphene.ObjectType):
class Meta:
interfaces = [relay.Node, ]
product_oscar = graphene.List(ProductNode)
productByID = DjangoFilterConnectionField(ProductNode)
def resolve_product_oscar(self, info, **kwargs):
return Product.objects.all()
产品节点:
class ProductNode(DjangoObjectType):
class Meta:
model = Product
interfaces = (relay.Node, )
filter_fields = {
"slug" : ['iexact']
}
PRODUCTLANDINGPAGEIMAGENODE:
class ProductLandingpageImageNode(DjangoObjectType):
class Meta:
model = ProductLandingpageImage
interfaces = (relay.Node, )
如何解决?
LANDINGPAGEIMAGE 模型应要求提供:
class AbstractProductLandingpageImage(models.Model):
"""
A landingpageimage of a product
"""
product = models.ForeignKey(
'catalogue.Product',
on_delete=models.CASCADE,
related_name='landingpage_image',
verbose_name=_("Product landingpage"))
date_created = models.DateTimeField(_("Date created"), auto_now_add=True)
original = models.ImageField(
_("Landingpage original"), upload_to=settings.OSCAR_IMAGE_FOLDER, max_length=255, blank=True)
ORDER_CHOICES = (
(1, 1),
(2, 2),
(3, 3),
(4, 4),
(5, 5),
(6, 6),
)
order = models.PositiveIntegerField(default=1, choices=ORDER_CHOICES, blank=True)
class Meta:
abstract = True
app_label = 'catalogue'
# Any custom models should ensure that this ordering is unchanged, or
# your query count will explode. See AbstractProduct.primary_image.
ordering = ["order"]
verbose_name = _('Product landingpage image')
verbose_name_plural = _('Product landingpage images')
Meta 中的默认排序在某种程度上不起作用。当我对订单值进行 Graphql 查询时,还有什么奇怪的不是返回“1, 2, 3...”而是“A_1, A_2...”
【问题讨论】:
-
你没有显示你的模型,但我猜你想在你的 ProductNode 中为你的 ProductLandingpageImage 模型创建一个明确的解析器,它有你想要的顺序,而不是使用默认的解析由 DjangoObjectType 创建的方法。
-
感谢您的回答。这正是我所需要的。
-
@MarkChackerian 你能告诉我如何写一个显式解析器吗?
标签: django graphql django-oscar graphene-python