question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Include the objects with org=None, while filtering serilaizer fileds based on the organization of an user

See original GitHub issue

https://github.com/openwisp/openwisp-users/blob/747ac34ac59c7fee843f8ad250cc5d12292c722b/openwisp_users/api/mixins.py#L137-L140

@nemesisdesign , @atb00ker with this line of code we are trying to filter out other ForeignKey related field other than organization field, based on the organization of a user, But mostly all of such fields has an option to create objects as shared org(organization=None). eg.:- templates, CA, etc… But this LOC only returns the object belonging to an organization, excluding the objects whose organization is set to None.

Will it not be better, if we include the shared objects along with these.

CC: @Purhan

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
nemesisdesigncommented, May 4, 2021

@ManishShah120 yes sorry my suggestion pointed to the wrong class but I believe you can use almost the exact same code. Try to use “in” instead of chaining several “or”, the result is easier to read.

You’re looping over the organizations and chaining several “or”, but can be done simply by adding None to the organization_filter list. It should be possible to disable this since it’s not required in every serializer. Please follow the suggestion I’ve given above.

1reaction
nemesisdesigncommented, May 4, 2021

@ManishShah120 @atb00ker @Purhan what do you think about something like the following:

diff --git a/openwisp_users/api/mixins.py b/openwisp_users/api/mixins.py
index 992223f..87fdfa2 100644
--- a/openwisp_users/api/mixins.py
+++ b/openwisp_users/api/mixins.py
@@ -14,6 +14,7 @@ class FilterByOrganization:
 
     permission_classes = [IsAuthenticated]
     organization_lookup = 'organization__in'
+    filter_shared = False
 
     @property
     def _user_attr(self):
@@ -26,8 +27,11 @@ class FilterByOrganization:
         return self.get_organization_queryset(qs)
 
     def get_organization_queryset(self, qs):
+        organization_list = getattr(self.request.user, self._user_attr)
+        if self.filter_shared:
+            organization_list = organization_list + [None]
         return qs.filter(
-            **{self.organization_lookup: getattr(self.request.user, self._user_attr)}
+            **{self.organization_lookup: organization_list}
         )
Read more comments on GitHub >

github_iconTop Results From Across the Web

Django rest framework - filtering for serializer field
When products have rendered into remote client, each of product takes a filed with filtered data. For example, model may be like this....
Read more >
Document how to handle permissions and filtering for related ...
I tried digging through the code, but I couldn't find an easy way to do this. Ideally, you should be able to call...
Read more >
3.0 Announcement - Django REST framework
We now use the following: Field is the base class for all fields. It does not include any default implementation for either serializing...
Read more >
Serializers - Django REST framework
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily ...
Read more >
Aggregation | Django documentation
Any filter() (or exclude() ) applied to normal model fields will have the effect of constraining the objects that are considered for aggregation....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found