在查询产品列表时,我需要显示产品信息以及变化。我的意思是我希望产品查询返回这样的东西
{
"data": {
"product": {
"id": "1",
"name": "Polo Shirt",
"productType": {
"name": "clothing"
},
"category": {
"name": "Men's"
},
"variants": [
{
"id": "1",
"name": "S",
"variant": [
{
"attribute": {
"name": "Size"
},
"values": [
{
"name": "S"
}
]
}
],
},
{
"id": "2",
"name": "XL",
"attributes": [
{
"attribute": {
"name": "Size"
},
"values": [
{
"name": "XL"
}
]
}
],
},
]
}
}
}
对于这样的结果,我的产品架构如下
type Query {
products: [Product]
}
type Product implements Node {
id: ID
productType: ProductType!
name: String!
slug: String!
description: String!
category: Category!
attributes: [SelectedAttribute]
variants: [ProductVariant]
articleNumber: String
barcode: String
}
type SelectedAttribute {
attribute: Attribute
values: [AttributeValue]
}
type Attribute implements Node {
id: ID
name: String
}
type AttributeValue implements Node {
id: ID
name: String
}
type ProductVariant implements Node {
id: ID!
name: String!
sku: String!
product: Product!
variant: [VariantAttributes]
}
type VariantAttributes {
attribute: Attribute
values: VariantAttribute
}
type VariantAttribute implements Node {
id: ID
name: String
slug: String
value: String
}
模型是
class Product(models.Model):
product_type = models.ForeignKey(
ProductType, related_name="products", on_delete=models.CASCADE
)
category = models.ForeignKey(
Category,
related_name="products",
on_delete=models.SET_NULL,
null=True,
blank=True,
)
name = models.CharField(max_length=128)
slug = models.SlugField()
description = models.TextField(blank=True)
class Attribute(ModelWithMetadata):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=250, unique=True, allow_unicode=True)
has_variants = models.BooleanField(default=False)
def __str__(self):
return self.name
class AttributeValue(SortableModel):
attribute = models.ForeignKey(Attribute, related_name="values", on_delete=models.CASCADE)
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=250, unique=True, allow_unicode=True)
value = models.CharField(max_length=100)
class ProductAttribute(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
attribute = models.ForeignKey(Attribute, on_delete=models.CASCADE)
def __str__(self):
return self.attribute.name
class VariantAttribute(models.Model):
attribute = models.ForeignKey(Attribute, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=250, unique=True, allow_unicode=True)
value = models.CharField(max_length=100)
def __str__(self):
return self.value
class ProductVariant(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="variants")
variant = models.ManyToManyField(VariantAttribute, related_name="productvariants")
name = models.CharField(max_length=255, blank=True)
sku = models.CharField(max_length=255, unique=True)
stock = models.PositiveIntegerField(default=0)
我的解析器正在关注
query = QueryType()
mutation = MutationType()
product = ObjectType('Product')
product_variant = ObjectType('ProductVariant')
@query.field('products')
@convert_kwargs_to_snake_case
def resolve_products(obj, info, **args):
products = models.Product.objects.all()
return products
@product.field('variants')
def resolve_product_variants(obj, info):
return models.ProductVariant.objects.filter(product=obj)
@product_variant.field('variant')
def resolve_attributes_of_product_variants(obj, info):
variant_attrs = models.Attribute.objects.filter(has_variants=True, productattribute__product=obj)
variant_attr_values = models.VariantAttribute.objects.none()
for attr in variant_attrs:
variant_attr_values |= models.VariantAttribute.objects.filter(attribute=attr)
print('variant_attrs', variant_attrs)
print('variant_attr_values', variant_attr_values)
return [
{
'attribute': variant_attrs,
'values': variant_attr_values
}
]
我不知道如何解决变体属性。请问有人可以帮我吗?