[Answer]-Traverse m2m with filter in django

0👍

Tested it and the solution is really simple.

With g=Activity.objects.filter(groups__in=[Group.objects.filter(group_nm=group_nm)],enddt__gt=now) you put an iterable in a list ([QuerySet]) which django doesn’t know how to index.

The solution is to dump the list comprehension:

g=Activity.objects.filter(groups__in=Group.objects.filter(group_nm=group_nm),enddt__gt=now)

and then it works like a charm.

👤RickyA

1👍

I think what you want to do is filter the activity_set (see backward relations) from the group specified by group_nm:

def group_details_page(request, group_nm):
    group_instance = Group.objects.get(group_nm=group_nm)
    activities = group_instance.activity_set.objects.filter(enddt__gt=now)
    return render_to_response(...etc...)
👤mVChr

Leave a comment