Home > OS >  Django: custom Action with date parameter not working
Django: custom Action with date parameter not working

Time:03-17

I have the following code in Python Django:

@action(detail=True, methods=['get'], url_path=r'by_date')
def get_meal_by_date(self, request, meal_date=date.today):
    print(meal_date)
    meals_by_date = Meal.objects.filter(
        owner=self.request.user,
        mealTimestamp__day=meal_date.day,
        mealTimestamp__month=meal_date.month,
        mealTimestamp__year=meal_date.year
    )
    serializer = self.get_serializer(meals_by_date, many=True)
    return Response(serializer.data, status=status.HTTP_200_OK)

That Code works on call it like this:

http://localhost:8000/meals/by_date/

My problem is how can I enrich the method to gets data back with date parameter.

For example with this call:

http://localhost:8000/meals/by_date/2022-03-16T21:30:45.290Z/

CodePudding user response:

What is your problem exactly, are you passing in the date in the url? That should work. path('adress/date:date/', something like this. In your view get the date.

  • Related