Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何重新排序Django Graphene子查询?_Django_Graphql_Django Oscar_Graphene Python - Fatal编程技术网

如何重新排序Django Graphene子查询?

如何重新排序Django Graphene子查询?,django,graphql,django-oscar,graphene-python,Django,Graphql,Django Oscar,Graphene Python,我正在尝试根据ProductLandingpageImage模型上的“订单”字段对ProductLandingPageImageNode重新排序 如果这是一个直接查询,我可以编写一个resolve方法,但我无法找出如何在子查询上实现这一点 主要查询: class Query(graphene.ObjectType): class Meta: interfaces = [relay.Node, ] product_oscar = graphene.List(ProductNode

我正在尝试根据ProductLandingpageImage模型上的“订单”字段对ProductLandingPageImageNode重新排序

如果这是一个直接查询,我可以编写一个resolve方法,但我无法找出如何在子查询上实现这一点

主要查询:

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()
PRODUCTNODE:

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')

元中的默认顺序不知何故不起作用。同样奇怪的是,当我用Graphql查询订单值时,返回的不是“1,2,3…”,而是“A_1,A_2…”

可能是这样的。由于您没有列出您的产品型号,我只是在产品上创建了引用图像的字段的名称,所以您应该重命名它。如果产品和图像之间存在多对一关系,则可能需要不同的字段名

import graphene
from graphene.django.types import DjangoObjectType

class ProductNode(DjangoObjectType):
    name_of_your_image_field = graphene.Field(ProductLandingpageImageNode)

    class Meta:
        model = Product
        ... other Meta data


    def resolve_name_of_your_image_field(self, info):  # rename to match field
        # Put the code that returns a single ProductLandingpageImage instance here
        # graphene-django will convert your ProductLandingPageImage instance into a ProductLandingpageImageNode
这用于返回单个
ProductLandingPageIMage
。如果要返回多个实例,请将字段定义更改为列表

    name_of_your_image_field = graphene.List(ProductLandingpageImageNode)

然后在您的解析程序中返回多个ProductLandingPageImage实例--例如,一个按您希望的方式排序的查询集。

您没有显示模型,但我猜您希望在ProductNode中为ProductLandingPageImage模型创建一个显式解析程序,该解析程序具有您想要的顺序,而不是使用DjangObjectType创建的默认解析方法。谢谢您的回答。这正是我需要的。@MarkChackerian你能告诉我如何编写显式解析器吗?