last day (14 days later) » 

11:11 AM
0
Q: Pass dict from views.py to list in serializer.py

62009030I'm kind new in django rest-framework. On my views I create a class ListUsers, which returns me a dict like this: list users = [ [{'is_target_user': False, 'user_id': 5, 'position': 3, 'points': 77}], [{'is_target_user': False, 'user_id': 7, 'position': 4, 'points': 66}], [{'is_target_user': Tr...

 
Its not a dict, its a list of list of dict, [[{}]], also the question is not very clear to me.
If you want to send something to serializer you can do it via get_serializer_context and then access it as kwargs['context']
 
@dnit13 I generated that "list of list of dict" and I want serialize it to json
 
an example of the output ?
 
output I want in json?
 
Yes , also show your serializer
 
11:11 AM
the serializer I couldn't create yet :/ is my problem
@dnit13 I eddited my question
Hi
 
Hi, How are you generating your current list ?
 
its with some calculations
 
On a queryset ?
 
with many queries
 
or with a serializer ?
 
11:16 AM
with many queries , im doing it on views.py
I can share code with you
On my views.py
i do many queries and add the ids to a list
than i start to compose the dict
 
That would be helpful
You can pass your extra context to a serializer and then in the serializer using methodserializerfield you can assign values to the fields by looking up from your extra context
 
im creating a serializer but i only receive one element
how i can receive all elements??
for element in users_list:
for e in element:
return Profile.objects.filter(user_id=e['user_id'])
 
aith the arguement many=True
 
on views?
 
11:32 AM
for the serializer
first store all ids in a list then do Profile.objects.filter(user_id__in=list_od_ids) to get all objects at once
Then pass this queryset to the serializer, with many=True arguement
 
oh nice
but how can i access other values from my previous dict?
cause now i can generate the data I have on DB by user id
but the data i had generate in the dictionary like points, position...
how can i ? :/
 
You have to send it to the serializer and then access it there
 
could you help me?
 
Hope this helps. Bye
 

last day (14 days later) »