-
Notifications
You must be signed in to change notification settings - Fork 149
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
How do I access the post object in a predicate? #118
Comments
After trying a few things, I don't think this is possible for a new object. I've resorted to only validating that they are authenticated, and I guess I'll build the permissions check for a new discussion into the create method of the serializer. |
Yes, it is not possible. The arguments Django passes to rules only contain the user and the object, and on create views specifically, not even the object (because there's none). I'm closing this as it's been a while and it also doesn't really have anything to do with rules, as this is Django's standard way of checking for the |
For DRF mixin, would something like this work? elif self.action == "create":
# Pass the permission a mock object that is not yet saved.
# This replicates the behavior of the CreateMixin.
tmp_serializer = self.get_serializer(
data=self.request.data
)
tmp_serializer.is_valid(raise_exception=False)
obj = model(**tmp_serializer.validated_data) It would also be possible to override But since the DRF is already plugging into ` EDIT: This actually doesn't work for more complex objects and because of: But passing the serialised data does work, with a couple of caveats:
|
I'm trying to check if a user posting a discussion has access to the topic they are trying to associate the discussion with. However when the user posts, the second argument in the predicate,
object
is alwaysNone
- I need to access the post body to check thetopic
attribute and compare to the users groups.How do I access the post object? Is it available in the predicate context somewhere if I bind the predicate?
This is the logic I have so far which works for reading discussion objects, but it looks like I need different logic for POST requests due to the object being null:
The text was updated successfully, but these errors were encountered: