0

I am using the filter in Django queryset, as it takes arguments like:

Model.objects.filter(name="Amit")

here is name and it's value, I am getting from the request of API:

def get(self, request):
    column = request.query_params.get('column')
    val = request.query_params.get('value')
    query = Data.objects.filter(column=val).values()
    serializer = DataSerializer(query, many=True).data
    return Response(serializer)

but here column [inside filter] is not getting recognized as the request parameter, it is taken as the column name of the table which is not available.

The error I got:

django.core.exceptions.FieldError: Cannot resolve keyword 'column' into field.

Hope this information is suitable for understanding the problem, if something is missing please let me know and kindly help me out with this.

1
  • Can you share your models please, the error means column isn't a field in your table. Commented Jul 14, 2022 at 7:48

1 Answer 1

2

You can try this: django-query-filter-with-variable-column

So if column is a variable containing which field you want to filter then it should be like this:

def get(self, request):
    column = request.query_params.get('column')
    val = request.query_params.get('value')
    query = Data.objects.filter(**{column:val}).values()
    serializer = DataSerializer(query, many=True).data
    return Response(serializer)

and you should check if column or val is not None for prevent error.

Sign up to request clarification or add additional context in comments.

Comments

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.