1
votes

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)

Output

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

I need fiter query for items where keywords are name from keyword model with query paraters 'Burger' as keyword my url is like http://127.0.0.1:8000/api/foodfeeds/?keywords=BURGER,teste&mood=happy&location=2323,7767.323&price=2

if keywords match in ItemVariation model in many to many fields so it should return itemvariation

2

2 Answers

2
votes

do the filtering like this:

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

or for a single keyword do this:

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

Not 100% sure if i understood your question correctly. Here i used a for loop on the list with they keywords, adding each keyword-Entity to the list - selected by name.

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)