Home > Software design >  get() missing 1 required positional argument: 'to_date'
get() missing 1 required positional argument: 'to_date'

Time:11-09

I am trying to filter the users that are created between certain dates, I am using the filter method. But i am having a problem passing the arguments, how can i pass the argument in a correct fashion, this is what i have tried so far.

views.py

class RegisteredUserFilter(APIView):
    serializer = RegisteredUserFilterSerializer
    
    def get(self, from_date, to_date):
        userondate = User.objects.filter(created_at__range=[from_date, to_date]).values()
        users = []
        for user in userondate:
            users.append(user['username'])
        return Response({"User": users})

serialzers.py

class RegisteredUserFilterSerializer(serializers.Serializer):
from_date = serializers.DateField()
to_date = serializers.DateField()
model = User

full code here: https://github.com/abinashkarki/rest_framework_authentication/tree/master/authapp

while doing this i am getting:

TypeError: get() missing 1 required positional argument: 'to_date'

How should the argument be passed?

CodePudding user response:

If u want to take args from get, u have to write it in your urlpattern. So, u have to write something like

path('userRegisteredOnDate/<from_date>/<to_date>/', RegisteredUserFilter.as_view()),

like in your password-reset urlpattern. And then you should parse it, because method will takes string type, not date type.

  • Related