Skip to content
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

Type in predicates when called from template #120

Open
kienerj opened this issue Apr 5, 2020 · 2 comments
Open

Type in predicates when called from template #120

kienerj opened this issue Apr 5, 2020 · 2 comments

Comments

@kienerj
Copy link

kienerj commented Apr 5, 2020

I'm using django-rest-framework and in a template I want to limit what a user can do by showing or not showing according action (a button) based on users permissions. It's a detail view showing all the child records. A user can edit the child record, if the user is the creator or the creators supervisor.
Hence in the predicates I compare current user to child record creator or supervisor. The predicates and rules work as expected.

The issue I'm facing is that when the permission is checked in the template and my predicates are called, the child record is passed in as OrderedDict and not as instance of it's model class. I suspect this is due to looping child instances in the template and the child instances are actually from a nested serializer? I'm speculating.

My current hack is to simply type-check inside the predicate.

Questions is, how can I solve this nicely? or what am I doing wrong?

@dfunckt
Copy link
Owner

dfunckt commented May 18, 2020

How do you make the comparison? Most of the times, and in this case in particular, it's best to compare the natural keys of the objects (usually the id attribute, but could be anything), instead of the object references. That is, instead of user is supervisor, it would be best if the check was user.id == supervisor.id

@kienerj
Copy link
Author

kienerj commented May 26, 2020

Since my template is sending a OrderedDict I have to use access via named fields like user["id"] and can't use dot notation.

I think this is probably due to using a django rest framework viewset based on a serializer.

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

No branches or pull requests

2 participants