Home > Net >  django queryset: How I can override values in django queryset before sending the response?
django queryset: How I can override values in django queryset before sending the response?

Time:10-28

I am on something where I need to override the values according to the URL parameters.

Model: Person

Name Alias
Alex {"Alex", "Jacob"

Alias column is the Array type

I get alias from the URL parameter like: example.com/?alias=Alex here alias = Alex

and my query is queryset = Person.objects.filter(name=alias)

and finally, I have to send the response to the frontend like <QuerySet [<Person: "Alex">]

basically here I have to remove Jacob from the Alias

I have tried

for qs in list(queryset):
    if "Alex" in qs["alias"]:
        qs["alias"] = ["Alex"]

it is working but I need to return response in the form of queryset object

CodePudding user response:

Queryset is a generator object, which once executed cannot be converted back.

Refer this official Django doc for more information.

CodePudding user response:

I was able to solve the issue

for qs in queryset:
    qs.alias = "Alex"
return queryset 
  • Related