问题
I am new to back-end programming especially in graphene-django. my question is how can we create filtered query in schema.py in graphene-django project without using Relay feature? i saw this before but i don't want to use Relay feature. rather than i want use a filter, but i don't know how?? now my models.py and schema.py look-like these:
*models.py
# shoes_store/ingredients/models.py
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=50)
notes = models.TextField(default='')
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=300)
descreption = models.TextField(default='')
price = models.CharField(max_length=50, default='')
imageURL = models.TextField(default='')
category= models.ForeignKey(Category,on_delete=models.CASCADE)
def __str__(self):
return self.name
and schema.py:
import graphene
from graphene_django.types import DjangoObjectType
from shoes_store.ingredients.models import Category, Product
class CategoryType(DjangoObjectType):
class Meta:
model = Category
class ProductType(DjangoObjectType):
class Meta:
model = Product
class Query:
#===========================
# product
products = graphene.List(ProductType)
product = graphene.Field(ProductType,product_id=graphene.ID())
def resolve_products(self, info, **kwargs):
# Querying a list
return Product.objects.all()
def resolve_product(self, info, product_id):
# Querying a single user
return Product.objects.get(pk=product_id)
#===========================
# product_category
categories = graphene.List(CategoryType)
category = graphene.Field(CategoryType,category_id=graphene.ID())
def resolve_categories(self, info, **kwargs):
# Querying a list
return Category.objects.all()
def resolve_category(self, info, category_id):
# Querying a single user
return Category.objects.get(pk=category_id)
回答1:
the best solution is look-like this if you don't want use Relay.
you can change your schema.py similar to:
import graphene
from graphene_django.types import DjangoObjectType
from shoes_store.ingredients.models import Category, Product
from django.db.models import Q
class CategoryType(DjangoObjectType):
class Meta:
model = Category
class ProductType(DjangoObjectType):
class Meta:
model = Product
class Query:
#===========================
# product
products = graphene.List(ProductType,name=graphene.String(),category_id=graphene.ID())
product = graphene.Field(ProductType,product_id=graphene.ID())
def resolve_products(self, info, name=None,category_id=None, **kwargs):
# Querying a list
if name:
filter = (
Q(name__icontains=name) |
Q(descreption__icontains=name)
)
return Product.objects.filter(filter)
#
if category_id:
filter = (
Q(category_id__exact=category_id)
)
return Product.objects.filter(filter)
#
return Product.objects.all()
def resolve_product(self, info, product_id):
# Querying a single user
return Product.objects.get(pk=product_id)
#===========================
# product_category
categories = graphene.List(CategoryType)
category = graphene.Field(CategoryType,category_id=graphene.ID())
def resolve_categories(self, info, **kwargs):
# Querying a list
return Category.objects.all()
def resolve_category(self, info, category_id):
# Querying a single user
return Category.objects.get(pk=category_id)
now you can use different queries in your "http://127.0.0.1:8000/graphql" address. fore example you can use:
query{
products{
id
name
price
}
}
or even:
query{
products(categoryId:1){
id
name
price
}
}
or
query{
products(name:"Men's boots"){
id
name
price
}
}
来源:https://stackoverflow.com/questions/61174897/how-we-can-make-filtered-query-in-schema-py-in-graphene-django-project-without-u