0

I want to display Customers from specific groups in the ListView, not able to understand how to get the queryset

class CustomerList(ListView):
  model = Customer
  queryset = Customer.objects.filter(member__groups__name__in=['online', 'whatsapp'])
  template_name = 'customer/customer_list.html'

models.py

class Customer(models.Model): 
    member = models.ForeignKey(User, verbose_name=_("Customer"), on_delete=models.CASCADE)
    contact = models.ForeignKey(Contact, verbose_name=_("Contact"), on_delete=models.CASCADE, blank=True, null=True)
    ...

Customers are added to the groups as below:

class AddUser(CreateView):
  def post(self, request, *args, **kwargs):
     form = UserForm(request.POST) 
     if form.is_valid(): 
        user = form.save(commit=False)
        group, created = Group.objects.get_or_create(name='online')
        user.groups.add(group)
        user.save()
Laxmikant
  • 2,046
  • 3
  • 30
  • 44
  • `__in` requires two flat dashes instead of one, does that fix it? – voodoo-burger Aug 27 '20 at 12:11
  • @voodoo-burger - sorry typo there, but it did not work as well – Laxmikant Aug 27 '20 at 12:15
  • The use of `related_name` is confusing here. What if you remove it, run migrations and use `user__groups__name__in`? – voodoo-burger Aug 27 '20 at 12:16
  • @voodoo-burger - let me try that – Laxmikant Aug 27 '20 at 12:17
  • Does this answer your question? [Django: How to filter Users that belong to a specific group](https://stackoverflow.com/questions/1810891/django-how-to-filter-users-that-belong-to-a-specific-group) – voodoo-burger Aug 27 '20 at 12:17
  • @voodoo-burger - already tried but no luck! That `queryset` only returns users whereas I want entire Customer object as there are certain other fields of the `Customer` model to be displayed on the template – Laxmikant Aug 27 '20 at 12:21
  • Uhm, if you are using this inside `Customer.objects.filter()` it can only return `Customer` objects... If you need more info about the user linked to the customer you can get it with `object.customer.x`. It's extremely confusing that you give your `Customer` model a field that is also named `customer`. – voodoo-burger Aug 27 '20 at 12:28
  • ok let me change it to resolve ur confustion – Laxmikant Aug 27 '20 at 12:30
  • What is the result of `print(Customer.objects.filter(member__groups__name__in=['online', 'whatsapp']).count())`? – JPG Aug 27 '20 at 12:33
  • @ArakkalAbu - Its zero (0). but I am sure that there are customers with `online` groups. and its not misspelled too – Laxmikant Aug 27 '20 at 12:40

1 Answers1

1

I have similar code working, check if this works for you -

class ProfessorsList(generic.list.ListView):
  model = Staff
  queryset = Staff.objects.filter(member__groups__name='teaching')

For multiple groups, you would do as: (I believe you're already doing it...)

Customer.objects.filter(member__groups__name__in=['online', ...])

If it still does not work for you try like this:

  users = User.objects.filter(groups__name__in=[your_groups])
  queryset = Customer.objects.filter(member__in=users)

Make sure customers have users and users are part of your_groups

trex
  • 3,848
  • 4
  • 31
  • 54