Try to search your question here, if you can't find : Ask Any Question Now ?

Django many-to-many query dict representation

HomeCategory: stackoverflowDjango many-to-many query dict representation
Avatargaurav asked 4 weeks ago

I have this query

        campaign_query = Campaign.objects.filter(id__in=campaign_ids).select_related('campaign_manager__name').prefetch_related('products').values(
            'id', 'name', 'campaign_manager_id', 'campaign_manager__name', 'products'
        )

Which is supposed to return a dict representation of my Campaign obj.

The problem is that ‘products’ is a many to many relationship and is only returning the first product, not a list of all of them.

Return example:

 <QuerySet [{'id': 19, 'name': 'Gby id', 'campaign_manager_id': 3, 'campaign_manager__name': '', 'products': 34}]>

What I want

 <QuerySet [{'id': 19, 'name': 'Gby id', 'campaign_manager_id': 3, 'campaign_manager__name': '', 'products': [34,35,36]}]>
1 Answers
Best Answer
AvatarMannu answered 4 weeks ago
Your Answer

8 + 3 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com