Skip to content

Allow DjangoObjectPermissions to use views that define get_queryset #2864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

ticosax
Copy link
Contributor

@ticosax ticosax commented Apr 22, 2015

Same as #2863 but for DjangoObjectPermissions.

@xordoquy xordoquy added this to the 3.1.2 Release milestone Apr 22, 2015
@xordoquy
Copy link
Collaborator

Looks good to me too.

@jpadilla
Copy link
Member

Shouldn't this instead behave like has_permission?

try:
    queryset = view.get_queryset()
except AttributeError:
    queryset = getattr(view, 'queryset', None)
except AssertionError:
    # view.get_queryset() didn't find .queryset
    queryset = None

I think there's also some docs that need to be updated.

@ticosax
Copy link
Contributor Author

ticosax commented Apr 26, 2015

There is no use case for DjangoObjectPermissions that doesn't support .queryset or .get_queryest(). Like it is today, DjangoObjectPermissions does not support queyset is None. So I think it should stay like that.
Regarding the documentation, I will update it tomorrow.
Thank you for the feedback.

@ticosax ticosax force-pushed the django-object-perm-get_queryset branch from 9df21d7 to 76bc42c Compare April 27, 2015 07:17
@@ -172,7 +172,7 @@ def get_required_object_permissions(self, method, model_cls):
return [perm % kwargs for perm in self.perms_map[method]]

def has_object_permission(self, request, view, obj):
model_cls = view.queryset.model
model_cls = view.get_queryset().model
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also fallback to view.queryset.model? That'd be consistent with the documentation above, and would be less likely to introduce any breakage to existing APIs.

Also then more consistent with #2863.

@tomchristie
Copy link
Member

One inline comment. Once that's addressed I'm happy with this.

@ticosax
Copy link
Contributor Author

ticosax commented May 4, 2015

the default implementation of .get_queryset() already serves .queryset
https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/generics.py#L47

This is why I think we do not need to fallback on view.queryset.

Therefore, for sake of consistency, I will do it the same way, by asserting the queryset exists, like it is done for DjangoModelPermissions https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/permissions.py#L123

@ticosax
Copy link
Contributor Author

ticosax commented May 4, 2015

I will re-open later . I need to work on this branch a bit more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants