Django过滤器查询来自许多字段

时间:2018-08-23 11:24:17

标签: python django django-models django-rest-framework django-views

models.py

class Keyword(models.Model):
        name=models.CharField(max_length=500,unique=True)
        image = models.ImageField(upload_to='keywords/', blank=True, null=True)
        mood=models.ManyToManyField(Mood,blank=True)
        def __str__(self):
            return str(self.name)

    class ItemVariation(models.Model):
        restaurant=models.ForeignKey(Restaurant,on_delete=models.CASCADE)
        items_id=models.ForeignKey(Item,on_delete=models.CASCADE)
        price=models.IntegerField(blank=True,null=True,default=0)
        item_code=models.CharField(max_length=500)
        keywords= models.ManyToManyField(Keyword)
        image=models.ImageField(upload_to='dishes/', blank=True, null=True)
        def __str__(self):
            return str(self.id)

views.py

class FoodfeedList(APIView):
    def get(self,request,format=None):
        keyword = request.GET['keywords'].split(",")
        mood=request.GET['mood']
        location=request.GET['location'].split(",")
        price=request.GET['price']
        user_id=request.user.id
        items=Item.objects.all()
        item_variation=ItemVariation.objects.all()
        search_restaurant=SearchRestaurant()
        search_result=search_restaurant.searchRes(location[0],location[1],keyword[0],user_id)
        all_results=[]
        json={}
        for i in search_result:
            json={}
            json['res_id'] = i['restaurant']['id']
          # keywords_list = ItemVariation.objects.filter(keywords=keyword[0])

            items=ItemVariation.objects.filter(restaurant = request.user.id)
            itemserializer=ItemVariationSerializer(items,many =True)
            json['itemserializer']=itemserializer.data
            all_results.append(json)
        # items_serilizer=ItemsSerializer(items,many=True)
        return Response(all_results)

输出

"res_id": 2,
"itemserializer": [
    {
        "id": 1,
        "price": 0,
        "item_code": "2134ffsd",
        "image": null,
        "restaurant": 1,
        "items_id": 1,
        "keywords": [1,2,3]
    },

我需要对项目进行更合适的查询,其中关键字是来自关键字模型的名称,且查询参数为“汉堡” 我的网址就像 http://127.0.0.1:8000/api/foodfeeds/?keywords=BURGER,teste&mood=happy&location=2323,7767.323&price=2

如果关键字在许多字段中的 ItemVariation 模型中匹配,则应返回itemvariation

2 个答案:

答案 0 :(得分:2)

执行以下过滤:

items=ItemVariation.objects.filter(keywords__name__in=keywords)

或针对单个关键字执行以下操作:

items=ItemVariation.objects.filter(keywords__name=keywords[0])

答案 1 :(得分:0)

不确定我是否正确理解了您的问题。在这里,我在列表上使用了带有关键字的for循环,将每个关键字-Entity添加到列表中-按名称选择。

class FoodfeedList(APIView):
def get(self,request,format=None):
    keywords = request.GET['keywords'].split(",")
    mood=request.GET['mood']
    location=request.GET['location'].split(",")
    price=request.GET['price']
    user_id=request.user.id
    keyword_names = []

    for keyword in keywords:
        keyword_names += Item.objects.get(name=keyword)

    item_variation=ItemVariation.objects.all()
    search_restaurant=SearchRestaurant()
    search_result=search_restaurant.searchRes(location[0],location[1],keyword[0],user_id)
    all_results=[]
    json={}
    for i in search_result:
        json={}
        json['res_id'] = i['restaurant']['id']
        json['keywords'] = keyword_names
        items=ItemVariation.objects.filter(restaurant = request.user.id)
        itemserializer=ItemVariationSerializer(items,many =True)
        json['itemserializer']=itemserializer.data
        all_results.append(json)
    # items_serilizer=ItemsSerializer(items,many=True)
    return Response(all_results)
相关问题