Reputation: 964
I have a django model such as this
id task_id status content_type content_encoding result date_done meta task_args
1 "9bb8b186-da5d-43b2-8fca-1496474b1cc9" "SUCCESS" "application/json" "utf-8" """done""" "2020-09-23 14:15:03.064283+05:30" "{""children"": []}" "('test_user', 1, ['test'], 'id_001')"
The task_args
field in the model is a text field
. How do I filter the model based on this data? Specifically I want the task_id
where task_args
is ('test_user', 1, ['test'], 'id_001')
This is my code so far
user = request.user.username
num = 1
parsed_data = eval("['test']")
id = "id_001"
print(type(parsed_data))
args = f"{(user,num,parsed_data,id)}"
print(args)
task_id = list(TaskResult.objects.filter(task_args=args).values('task_id'))
Right now this code outputs an empty list
Not sure what I am doing wrong.
Is this because I am passing the tuple
as a string
?
I am passing this as a string
because the task_args
field is a text field. I also tried passing this as a tuple
. Even that results in an empty list
.
Upvotes: 0
Views: 178
Reputation: 88589
seems like a typo somewhere in your code. I did a similar test and got succeeded.
In [33]: Foo.objects.create(task_args="('test_user', 1, ['test'], 'id_001')")
Out[33]: <Foo: Foo object (4)>
In [34]: Foo.objects.all()
Out[34]: <QuerySet [<Foo: Foo object (4)>]>
In [35]: tuple_data = ('test_user',1,["test"],"id_001") # create a tuple
In [36]: str_tuple_data = str(tuple_data) # convert to a string
In [37]: str_tuple_data
Out[37]: "('test_user', 1, ['test'], 'id_001')"
In [38]: Foo.objects.filter(task_args=str_tuple_data)
Out[38]: <QuerySet [<Foo: Foo object (4)>]>
In [39]: Foo.objects.filter(task_args=str_tuple_data).values()
Out[39]: <QuerySet [{'id': 4, 'task_args': "('test_user', 1, ['test'], 'id_001')"}]>
Upvotes: 1