Here is the error that am getting when I try to retrieve data
DatabaseError at /foods/41007428/65bffaaab212b521886f0e47/
No exception message supplied
Request Method: GET Request URL: http://127.0.0.1:8000/foods/41007428/65bffaaab212b521886f0e47/
Django Version: 4.1.13
Exception Type: DatabaseError
Exception Location: C:\Users\omuya\projects\food_backend\foodenv\Lib\site-packages\djongo\cursor.py, line 59, in execute
Raised during: foods.views.foodByCategoryandCode
Python Executable: C:\Users\omuya\projects\food_backend\foodenv\Scripts\python.exe
Python Version: 3.12.1
Python Path:
['C:\Users\omuya\projects\food_backend\food_api_project',
'C:\Users\omuya\AppData\Local\Programs\Python\Python312\python312.zip',
'C:\Users\omuya\AppData\Local\Programs\Python\Python312\DLLs',
'C:\Users\omuya\AppData\Local\Programs\Python\Python312\Lib',
'C:\Users\omuya\AppData\Local\Programs\Python\Python312',
'C:\Users\omuya\projects\food_backend\foodenv',
'C:\Users\omuya\projects\food_backend\foodenv\Lib\site-packages']
Server time: Mon, 05 Feb 2024 23:09:11 +0000
The above exception ( Keyword: None Sub SQL: None FAILED SQL: ('SELECT "foods_product"."_id", "foods_product"."title", "foods_product"."foodTags", "foods_product"."foodType", "foods_product"."code", "foods_product"."isAvailable", "foods_product"."restaurant_id", "foods_product"."rating", "foods_product"."ratingCount", "foods_product"."description", "foods_product"."price", "foods_product"."additives", "foods_product"."imageUrl", "foods_product"."category_id", "foods_product"."time", "foods_product"."created_at", "foods_product"."updated_at" FROM "foods_product" WHERE ("foods_product"."category_id" = %(0)s AND "foods_product"."code" = %(1)s AND "foods_product"."isAvailable")',) Params: (('65bffaaab212b521886f0e47', '41007428'),) Version: 1.3.6) was the direct cause of the following exception:
I just wanted to get filtered according to code and category
@api_view(['GET'])
def foodByCategoryandCode(request,catid,code):
foods = Product.objects.filter(category__pk = catid, code = code,isAvailable = True)
serializer = ProductSerializer(foods, many=True)
return Response(serializer.data)
This my url for retrieving the data
path('foods/<str:code>/<str:catid>/', foodByCategoryandCode, name='random-by-code-and-category'),
class Product(models.Model):
_id = models.CharField(primary_key=True, editable=False,max_length=255)
title = models.CharField(max_length=100, unique=True)
foodTags = models.JSONField(default = [])
foodType = models.JSONField(default = [])
code = models.CharField(max_length=10)
isAvailable = models.BooleanField()
restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE)
rating = models.DecimalField(max_digits=3, decimal_places=1,validators=[MinValueValidator(1), MaxValueValidator(5)])
ratingCount = models.CharField(max_length=10)
description = models.TextField()
price = models.DecimalField(max_digits=5, decimal_places=2)
additives = models.JSONField(default = [])
imageUrl = models.JSONField(default = [])
category = models.ForeignKey(Category, on_delete=models.CASCADE)
time = models.CharField(max_length=10)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
if not self._id:
self._id = str(ObjectId())
super().save(*args, **kwargs)`
Tried this in my views and it worked but don't if was the correct approach, since the problem was filtering by bool
=
isAvailable
@api_view(['GET'])
def foodByCategoryAndCode(request,catid,code):
foods = Product.objects.filter(Q(code=code, category=catid))
_foods = [food for food in foods if food.isAvailable == True]
serializer = ProductSerializer(_foods, many=True)
return Response(serializer.data)