django-advanced-filters icon indicating copy to clipboard operation
django-advanced-filters copied to clipboard

Feature Request: allow toggling sharing for user/group on and off

Open sirmaahe opened this issue 8 years ago • 6 comments

Hello!

I have custom User model, which inherits AbstractBaseUser and doesn't have Group relation. So when i use filter mixin, i get this exception:

Traceback (most recent call last):
  File "python2.7/site-packages/advanced_filters/admin.py", line 34, in lookups
    return AdvancedFilter.objects.filter_by_user(request.user).filter(
  File "python2.7/site-packages/advanced_filters/models.py", line 13, in filter_by_user
    return self.filter(Q(users=user) | Q(groups__in=user.groups.all()))
  File "python2.7/site-packages/django/utils/functional.py", line 235, in inner
    return func(self._wrapped, *args)
AttributeError: 'User' object has no attribute 'groups'

Can you add a attribute checking in UserLookupManager.filter_by_user or something?

sirmaahe avatar Sep 28 '17 08:09 sirmaahe

Yep that's a good idea, and we should also allow toggling the sharing (and related filtering) for user/group in settings.

P.s: Pull requests are welcome 😄

asfaltboy avatar Sep 28 '17 08:09 asfaltboy

Is there a workaround for that?

sbrunner avatar Jan 26 '19 12:01 sbrunner

Hi @sbrunner , try adding the attribute checking suggested by @sirmaahe , on this method :

https://github.com/modlinltd/django-advanced-filters/blob/2fe81aac5d43e618623254c7a005d1e643ff7f39/advanced_filters/models.py#L9-L13

I.e:

 advanced_filters/models.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/advanced_filters/models.py b/advanced_filters/models.py
index 2c9f749..f19c532 100644
--- a/advanced_filters/models.py
+++ b/advanced_filters/models.py
@@ -9,7 +9,8 @@ from .q_serializer import QSerializer
 class UserLookupManager(models.Manager):
     def filter_by_user(self, user):
         """All filters that should be displayed to a user (by users/group)"""
-
+        if not hasattr(user, 'groups'):
+            return self.filter(users=user)
         return self.filter(Q(users=user) | Q(groups__in=user.groups.all()))

If it works for you, give back to the community by submitting a PR (with a unit test please, and some docs update would be nice).

asfaltboy avatar Jan 28 '19 06:01 asfaltboy

OK, Thanks :-)

sbrunner avatar Jan 28 '19 08:01 sbrunner

can any one help me for my query. Please

Table 1 
User   and  id  [pk], username

Table 2
group  and id [pk], group_name

Table 2
user_groups and id[pk],user_id [fk  of Table1]  , group_id,[fk of Table2]

how i get how i get all users and their user group??

eg.

Table 1 
 id  , username
1 ,xyz
2,abc
3,lmn

Table 2
 id , group_name
1, admin
2, user
3,staff
Table 2
 id[pk],user_id , group_id
1  ,        1,              1
2,         2               2
3          3               1

expected result



1 ,xyz , admin
2,abc, user
3,lmn,staff

i one django query , i can do with raw query but i need in django style query please help me

i do as follow but failed

result  =   User.groups.objects.all()
but it give me error
'ManyToManyDescriptor' object has no attribute 'objects'

please tell me, from django admin related table , how can i show all user and their user group report please

Rajani-salvi avatar Nov 03 '20 08:11 Rajani-salvi

I created a PR for it. It was easy enough. I want to use this plugin maybe. I've been playing around with it. It's quite nice, however a bit complex to use. Ok for developers but more complex for managers.

But I understand that it's not easy to make a user-friendly UI in the Django admin. It's not great but with a bit of modification and plugins it is just good enough to not make a custom admin yourself. At least it saves some work for as long as it suffices.

So thanks for the plugin. I hope this PR is will be accepted and that the plugin will be accessible to more people. Maybe I'll do some more hacking on this some time.

Cheers

gitaarik avatar Sep 30 '21 21:09 gitaarik