My Code
def get_areas(self, obj):
text = []
if obj.areas and not obj.areas == []:
print(json.loads(obj.area_count))
print(type(obj.area_count))
return json.loads(obj.area_count)
return "-"
get_areas.short_description = "Areas"
get_areas.admin_order_field= "area_count"
def get_queryset(self, request):
from django.contrib.postgres.fields.jsonb import KeyTextTransform
qs = super(UserAdmin, self).get_queryset(request)
filtered_query = qs.filter(
is_superuser=False, is_staff=False
).annotate(
wishlist_count=Count('wishlist', distinct=True),
booking_count=Count('booking', distinct=True),
review_count=Count('review', distinct=True),
suggestion_count=Count('suggestion', distinct=True),
home_city=NullIf('home__city'),
home_country=NullIf('home__country'),
area_count = F('areas'),
)
What i get is a list as a str type which looks something like:
"[{'id': 98, 'name': 'Khalifa City and Masdar City', 'city': 'Abu Dhabi'},
{'id': 99, 'name': 'Masdar City', 'city': 'Abu Dhabi'}]"
<class 'str'>
What I want is the list of names instead:
['Khalifa City and Masdar City','Madsar City']
So, how to return the result by area_count = F('areas')
as list instead of str
Note: The list can contain more than 1 dictionary and I want to return just the names.Any effort would be appreciated
What do you need an annotate
by F("areas")
for?
Just remove it and access "areas" as an attr on the object in QS.