python相关字段的查找无效:name

watbbzwu  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(182)

我试图在我的apiview上实现q搜索,但它显示无效的查找名称,这很奇怪。我已经根据模型的字段添加了搜索字段。
我的看法是:

from django.db.models import Q
class PrdouctSearchAPIView(ListAPIView):
    permission_classes = [AllowAny]   

    # def list(self, request, *args,**kwargs):
    def get(self, request, *args,**kwargs):
        qur = self.request.query_params.get('search')

        item = Product.objects.filter(Q(category__name__icontains=qur)|
                                       Q(brand__name__icontains=qur)|
                                       Q(description__icontains=qur)|
                                       Q(collection__name__icontains=qur)|
                                       Q(variants__name__icontains=qur))
        serializer = ProductSerializer(item,many=True)
        return Response(serializer.data)

我的模型:

class Product(models.Model):

    merchant = models.ForeignKey(Seller,on_delete=models.CASCADE,blank=True,null=True)
    category = models.ManyToManyField(Category, blank=False)
    sub_category = models.ForeignKey(Subcategory, on_delete=models.CASCADE,blank=True,null=True)
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
    collection = models.ForeignKey(Collection, on_delete=models.CASCADE)
    featured = models.BooleanField(default=False)  # is product featured?
    description = RichTextField(blank=True)
    variants = models.ManyToManyField(Variants,related_name='products')

class Category(models.Model):
    #parent = models.ForeignKey('self',related_name='children',on_delete=models.CASCADE,blank=True,null=True)
    name = models.CharField(max_length=100, unique=True)
    image = models.ImageField(null=True, blank=True)  

class Brand(models.Model):
    brand_category = models.ManyToManyField(Category,blank=True,null=True)
    name = models.CharField(max_length=100, unique=True)  

class Collection(models.Model):
    name = models.CharField(max_length=100, unique=True)
    image = models.ImageField(null=True, blank=True)

我的网址是:

path('api/productsearch',views.PrdouctSearchAPIView.as_view(),name='api-productsearch'),

正如我们可以看到的,有字段“category\uuuu name”,这样的字段不仅是“name”,而且错误表示查找“name”无效。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题