last day (14 days later) » 

7:03 AM
0
A: How to set permission as per user using Django and Python

Arpit SolankiThe meta class defined is just a representation that your model will have only these permissions so it becomes manageable. You can create permissions programmatically using Permission class from django. From django docs User objects have two many-to-many fields: groups and user_permissions. ...

 
Can you edit as per my permission set. In which file i will define the first portion of your code and BlogPost is the app name right ? Here I need to set three different permission as per different user.
 
@satya see the edit. BlogPost is the app name. Also you need to grant permission to each user explicitly using Permissions model. After migrate get the Permission object and use it to grant a permission to user.
 
The permission will be set inside the admin page not any logged in user and you mean from django.contrib.auth.models import Permission, User def grant_permissions(request): user = request.user content_type = ContentType.objects.get_for_model(plant) permission = Permission.objects.get( codename='change_plant', content_type=content_type, ) user.user_permissions.add(permission), I need to write like this in views.py.
 
If you want to set permissions in the admin then you have to write some more code to setup admin page and other things but for in general views the above will work. In case of admin you have to get the user from User model and then set the permissions with logic above.
 
because for any registered user admin will set the permission. User can not give any permission to ownshelf. It was my requirement actually and those permissions will be checked using decorator function.
 
7:03 AM
The above is a general solution to the question asked. If you have requirement for admin page then I am afraid to say this but you have to ask another question. General solution is this one and works as expected
 
Hi
 
Yes I can understand
But if I have like this
class Control(models.Model):
"""docstring for Control"""

user_id = models.ForeignKey(User)
control_reactor = models.IntegerField(default=0)
find_reactor = models.IntegerField(default=0)
view_reactor = models.IntegerField(default=0)
 
You did not mention anything about the admin page so I figured its not necessary to mentio
Are you using this model for permissions?
 
Then also admin can set the permission but Can you please say how I will access those using decorator function ?
I have posted the model class
 
7:06 AM
The model Control is not needed. The default Permission Model will do this.
 
Lets say for user-1 control_reactor =1,find_reactor=1 and view_reactor=0 then user-1 can only aceess the control_reactor and find_reactor functionality
Yes thats actully I need using the decorator function like @permission_required
 
Ok I will tell you a simple solution.
 
yes please
 
Define all the permission in the Meta class like you posted in the question
migrate the database
 
I have done that
Migrated also
 
7:09 AM
then all the permission will be created in Permission Model
 
But I was confised how to change permission user-user
 
Now all you need to do is get the user, get the Permission object using objects.get method and add the permission to the user.
If you are on admin then also you can do same just get the user_id and permission and add it to user
 
let me to send the screen shot of admin page after migrate
Ok I will try it
Thanks for your help
 

last day (14 days later) »