0

I am trying to filter CartItem objects based on their delivery dates. I define a startfilterdate and an endfilterdate that i format as a string in the same way my delivery_date's are formatted. Not sure what i am doing wrong.

models.py

class CartItems(models.Model):
    restaurant = models.ForeignKey(Restaurant, related_name='restaurant', on_delete=models.CASCADE)
    delivery_date = models.DateField(auto_now_add=False)

views.py

class RestaurantOrders(generics.ListAPIView):
    serializer_class = RestaurantOrderSerializer

    def get_queryset(self):
        restaurant_id = self.kwargs['pk']
        startfilterdate = date.today()
        startfilterdate = startfilterdate.strftime("%Y-%m-%d")
        endfilterdate = date.today()+timedelta(days=9)
        endfilterdate = endfilterdate.strftime("%Y-%m-%d")
        orders = CartItems.objects.filter(restaurant_id = restaurant_id, delivery_date=[startfilterdate,endfilterdate])

Error: TypeError: expected string or bytes-like object

1 Answer 1

1

Use range lookup as,

orders = CartItems.objects.filter(..., delivery_date__range=[startfilterdate,endfilterdate])
Sign up to request clarification or add additional context in comments.

1 Comment

ahhhh i see, now all the other answers i read make sense, didnt understand the syntax, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.