How can I write my own permissions in django
I want to write my own permissions in Django, I mean I want to define exactly what a user can or cannot do, I have read this enter link description here
but it seems change_task_status is sth predefined in Django. for example, I want to define exactly users can have access to just get method of a view and just from row 1 to 8 of the database table, and sth like this. How can I do this?
Edit:
First of all, I did this with default permissions that are in auth_permission table in Django, for each model it creates permissions of add/view/change/delete in this table and I know that I can use it for my purpose. but I have two problems, first I don't want to use the default permission class od Django.contrib,auth model thus I want to create my own permission table (instead of auth_permissions I have mapp_permissions) it makes a problem for me now this new table is not filled with default permissions so I need to define permissions myself I mean I have to say what add_modelname means and also after I do this I need to define some new permissions that say for example for one model:user_x have permission view_modelname, users also have this permission but from data of this model which stored in database user_y just can see records of 1 to 8 of db table not all
Edit 2:
as you can see in permissions class comment it says:"it's
not currently possible to say "Mary may only change news stories that have a
certain status or publication date.""
how can I make it possible?
also, there should be a code inside Django files that define for the machine for example add_user which is in the table means what
python django django-rest-framework
|
show 5 more comments
I want to write my own permissions in Django, I mean I want to define exactly what a user can or cannot do, I have read this enter link description here
but it seems change_task_status is sth predefined in Django. for example, I want to define exactly users can have access to just get method of a view and just from row 1 to 8 of the database table, and sth like this. How can I do this?
Edit:
First of all, I did this with default permissions that are in auth_permission table in Django, for each model it creates permissions of add/view/change/delete in this table and I know that I can use it for my purpose. but I have two problems, first I don't want to use the default permission class od Django.contrib,auth model thus I want to create my own permission table (instead of auth_permissions I have mapp_permissions) it makes a problem for me now this new table is not filled with default permissions so I need to define permissions myself I mean I have to say what add_modelname means and also after I do this I need to define some new permissions that say for example for one model:user_x have permission view_modelname, users also have this permission but from data of this model which stored in database user_y just can see records of 1 to 8 of db table not all
Edit 2:
as you can see in permissions class comment it says:"it's
not currently possible to say "Mary may only change news stories that have a
certain status or publication date.""
how can I make it possible?
also, there should be a code inside Django files that define for the machine for example add_user which is in the table means what
python django django-rest-framework
What do you meanand just from row 1 to 8 of the database table
? That example above shows you a template for writing a permission. You define permissions on the models, and then check if that user has the necessary permission using the function? You can apply the permissions to users (the rows 1 - 8, I assume) via the dashboard or via a migration.
– Rodney Hawkins
21 hours ago
please check my edit
– Annabelle
20 hours ago
@Annabelle not sure why you wouldn't want to use the Django auth permissions table but if you do want to use a custom permissions table, you can no longer use the usual permissions defined in the model META class because those ones are added to the auth permissions table during migration. You would have to roll out a custom solution. As for your second requirement of row 1-8, it's also a strange one and does not fall into the type of permissions offered by the current permissions framework, but since you will be rolling out a new logic anyway, you can also implement that on your own
– Ken4scholars
20 hours ago
1
@Annabelle What I mean is that is that you can write a DRF permissions class which checks that the user has the permission to change objects of a particular status. The check could be on whether the user is admin or normal user. But then again if you need it to be fine-grained, you can create a separate class for the permissions and define as many as is needed. In essence, what I mean is that you can have them work side by side with the original permissions framework and not replace it
– Ken4scholars
19 hours ago
1
@Annabelle you should import all your models in the__init__.py
file of the package. Check this out stackoverflow.com/questions/5534206/…
– Ken4scholars
19 hours ago
|
show 5 more comments
I want to write my own permissions in Django, I mean I want to define exactly what a user can or cannot do, I have read this enter link description here
but it seems change_task_status is sth predefined in Django. for example, I want to define exactly users can have access to just get method of a view and just from row 1 to 8 of the database table, and sth like this. How can I do this?
Edit:
First of all, I did this with default permissions that are in auth_permission table in Django, for each model it creates permissions of add/view/change/delete in this table and I know that I can use it for my purpose. but I have two problems, first I don't want to use the default permission class od Django.contrib,auth model thus I want to create my own permission table (instead of auth_permissions I have mapp_permissions) it makes a problem for me now this new table is not filled with default permissions so I need to define permissions myself I mean I have to say what add_modelname means and also after I do this I need to define some new permissions that say for example for one model:user_x have permission view_modelname, users also have this permission but from data of this model which stored in database user_y just can see records of 1 to 8 of db table not all
Edit 2:
as you can see in permissions class comment it says:"it's
not currently possible to say "Mary may only change news stories that have a
certain status or publication date.""
how can I make it possible?
also, there should be a code inside Django files that define for the machine for example add_user which is in the table means what
python django django-rest-framework
I want to write my own permissions in Django, I mean I want to define exactly what a user can or cannot do, I have read this enter link description here
but it seems change_task_status is sth predefined in Django. for example, I want to define exactly users can have access to just get method of a view and just from row 1 to 8 of the database table, and sth like this. How can I do this?
Edit:
First of all, I did this with default permissions that are in auth_permission table in Django, for each model it creates permissions of add/view/change/delete in this table and I know that I can use it for my purpose. but I have two problems, first I don't want to use the default permission class od Django.contrib,auth model thus I want to create my own permission table (instead of auth_permissions I have mapp_permissions) it makes a problem for me now this new table is not filled with default permissions so I need to define permissions myself I mean I have to say what add_modelname means and also after I do this I need to define some new permissions that say for example for one model:user_x have permission view_modelname, users also have this permission but from data of this model which stored in database user_y just can see records of 1 to 8 of db table not all
Edit 2:
as you can see in permissions class comment it says:"it's
not currently possible to say "Mary may only change news stories that have a
certain status or publication date.""
how can I make it possible?
also, there should be a code inside Django files that define for the machine for example add_user which is in the table means what
python django django-rest-framework
python django django-rest-framework
edited 20 hours ago
asked 21 hours ago
Annabelle
1147
1147
What do you meanand just from row 1 to 8 of the database table
? That example above shows you a template for writing a permission. You define permissions on the models, and then check if that user has the necessary permission using the function? You can apply the permissions to users (the rows 1 - 8, I assume) via the dashboard or via a migration.
– Rodney Hawkins
21 hours ago
please check my edit
– Annabelle
20 hours ago
@Annabelle not sure why you wouldn't want to use the Django auth permissions table but if you do want to use a custom permissions table, you can no longer use the usual permissions defined in the model META class because those ones are added to the auth permissions table during migration. You would have to roll out a custom solution. As for your second requirement of row 1-8, it's also a strange one and does not fall into the type of permissions offered by the current permissions framework, but since you will be rolling out a new logic anyway, you can also implement that on your own
– Ken4scholars
20 hours ago
1
@Annabelle What I mean is that is that you can write a DRF permissions class which checks that the user has the permission to change objects of a particular status. The check could be on whether the user is admin or normal user. But then again if you need it to be fine-grained, you can create a separate class for the permissions and define as many as is needed. In essence, what I mean is that you can have them work side by side with the original permissions framework and not replace it
– Ken4scholars
19 hours ago
1
@Annabelle you should import all your models in the__init__.py
file of the package. Check this out stackoverflow.com/questions/5534206/…
– Ken4scholars
19 hours ago
|
show 5 more comments
What do you meanand just from row 1 to 8 of the database table
? That example above shows you a template for writing a permission. You define permissions on the models, and then check if that user has the necessary permission using the function? You can apply the permissions to users (the rows 1 - 8, I assume) via the dashboard or via a migration.
– Rodney Hawkins
21 hours ago
please check my edit
– Annabelle
20 hours ago
@Annabelle not sure why you wouldn't want to use the Django auth permissions table but if you do want to use a custom permissions table, you can no longer use the usual permissions defined in the model META class because those ones are added to the auth permissions table during migration. You would have to roll out a custom solution. As for your second requirement of row 1-8, it's also a strange one and does not fall into the type of permissions offered by the current permissions framework, but since you will be rolling out a new logic anyway, you can also implement that on your own
– Ken4scholars
20 hours ago
1
@Annabelle What I mean is that is that you can write a DRF permissions class which checks that the user has the permission to change objects of a particular status. The check could be on whether the user is admin or normal user. But then again if you need it to be fine-grained, you can create a separate class for the permissions and define as many as is needed. In essence, what I mean is that you can have them work side by side with the original permissions framework and not replace it
– Ken4scholars
19 hours ago
1
@Annabelle you should import all your models in the__init__.py
file of the package. Check this out stackoverflow.com/questions/5534206/…
– Ken4scholars
19 hours ago
What do you mean
and just from row 1 to 8 of the database table
? That example above shows you a template for writing a permission. You define permissions on the models, and then check if that user has the necessary permission using the function? You can apply the permissions to users (the rows 1 - 8, I assume) via the dashboard or via a migration.– Rodney Hawkins
21 hours ago
What do you mean
and just from row 1 to 8 of the database table
? That example above shows you a template for writing a permission. You define permissions on the models, and then check if that user has the necessary permission using the function? You can apply the permissions to users (the rows 1 - 8, I assume) via the dashboard or via a migration.– Rodney Hawkins
21 hours ago
please check my edit
– Annabelle
20 hours ago
please check my edit
– Annabelle
20 hours ago
@Annabelle not sure why you wouldn't want to use the Django auth permissions table but if you do want to use a custom permissions table, you can no longer use the usual permissions defined in the model META class because those ones are added to the auth permissions table during migration. You would have to roll out a custom solution. As for your second requirement of row 1-8, it's also a strange one and does not fall into the type of permissions offered by the current permissions framework, but since you will be rolling out a new logic anyway, you can also implement that on your own
– Ken4scholars
20 hours ago
@Annabelle not sure why you wouldn't want to use the Django auth permissions table but if you do want to use a custom permissions table, you can no longer use the usual permissions defined in the model META class because those ones are added to the auth permissions table during migration. You would have to roll out a custom solution. As for your second requirement of row 1-8, it's also a strange one and does not fall into the type of permissions offered by the current permissions framework, but since you will be rolling out a new logic anyway, you can also implement that on your own
– Ken4scholars
20 hours ago
1
1
@Annabelle What I mean is that is that you can write a DRF permissions class which checks that the user has the permission to change objects of a particular status. The check could be on whether the user is admin or normal user. But then again if you need it to be fine-grained, you can create a separate class for the permissions and define as many as is needed. In essence, what I mean is that you can have them work side by side with the original permissions framework and not replace it
– Ken4scholars
19 hours ago
@Annabelle What I mean is that is that you can write a DRF permissions class which checks that the user has the permission to change objects of a particular status. The check could be on whether the user is admin or normal user. But then again if you need it to be fine-grained, you can create a separate class for the permissions and define as many as is needed. In essence, what I mean is that you can have them work side by side with the original permissions framework and not replace it
– Ken4scholars
19 hours ago
1
1
@Annabelle you should import all your models in the
__init__.py
file of the package. Check this out stackoverflow.com/questions/5534206/…– Ken4scholars
19 hours ago
@Annabelle you should import all your models in the
__init__.py
file of the package. Check this out stackoverflow.com/questions/5534206/…– Ken4scholars
19 hours ago
|
show 5 more comments
1 Answer
1
active
oldest
votes
According to Edit 2 , I see that you have some business logic related to permissions check , have a look at django-rules , I think it's what you're looking for.
Thank you, now I have a little problem can you help me with it first?"another problem I have is this that for purpose of my project I used a model folder instead of model.py file, now permission class cannot auto-generate add/change/delete/update of my models, how can I define where to look for my model?"
– Annabelle
19 hours ago
1
Did you import the models inside the init.py in the models forlder ? docs.djangoproject.com/en/2.1/topics/db/models/…
– Haidar Zeineddine
19 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53942754%2fhow-can-i-write-my-own-permissions-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
According to Edit 2 , I see that you have some business logic related to permissions check , have a look at django-rules , I think it's what you're looking for.
Thank you, now I have a little problem can you help me with it first?"another problem I have is this that for purpose of my project I used a model folder instead of model.py file, now permission class cannot auto-generate add/change/delete/update of my models, how can I define where to look for my model?"
– Annabelle
19 hours ago
1
Did you import the models inside the init.py in the models forlder ? docs.djangoproject.com/en/2.1/topics/db/models/…
– Haidar Zeineddine
19 hours ago
add a comment |
According to Edit 2 , I see that you have some business logic related to permissions check , have a look at django-rules , I think it's what you're looking for.
Thank you, now I have a little problem can you help me with it first?"another problem I have is this that for purpose of my project I used a model folder instead of model.py file, now permission class cannot auto-generate add/change/delete/update of my models, how can I define where to look for my model?"
– Annabelle
19 hours ago
1
Did you import the models inside the init.py in the models forlder ? docs.djangoproject.com/en/2.1/topics/db/models/…
– Haidar Zeineddine
19 hours ago
add a comment |
According to Edit 2 , I see that you have some business logic related to permissions check , have a look at django-rules , I think it's what you're looking for.
According to Edit 2 , I see that you have some business logic related to permissions check , have a look at django-rules , I think it's what you're looking for.
answered 19 hours ago
Haidar Zeineddine
7325
7325
Thank you, now I have a little problem can you help me with it first?"another problem I have is this that for purpose of my project I used a model folder instead of model.py file, now permission class cannot auto-generate add/change/delete/update of my models, how can I define where to look for my model?"
– Annabelle
19 hours ago
1
Did you import the models inside the init.py in the models forlder ? docs.djangoproject.com/en/2.1/topics/db/models/…
– Haidar Zeineddine
19 hours ago
add a comment |
Thank you, now I have a little problem can you help me with it first?"another problem I have is this that for purpose of my project I used a model folder instead of model.py file, now permission class cannot auto-generate add/change/delete/update of my models, how can I define where to look for my model?"
– Annabelle
19 hours ago
1
Did you import the models inside the init.py in the models forlder ? docs.djangoproject.com/en/2.1/topics/db/models/…
– Haidar Zeineddine
19 hours ago
Thank you, now I have a little problem can you help me with it first?"another problem I have is this that for purpose of my project I used a model folder instead of model.py file, now permission class cannot auto-generate add/change/delete/update of my models, how can I define where to look for my model?"
– Annabelle
19 hours ago
Thank you, now I have a little problem can you help me with it first?"another problem I have is this that for purpose of my project I used a model folder instead of model.py file, now permission class cannot auto-generate add/change/delete/update of my models, how can I define where to look for my model?"
– Annabelle
19 hours ago
1
1
Did you import the models inside the init.py in the models forlder ? docs.djangoproject.com/en/2.1/topics/db/models/…
– Haidar Zeineddine
19 hours ago
Did you import the models inside the init.py in the models forlder ? docs.djangoproject.com/en/2.1/topics/db/models/…
– Haidar Zeineddine
19 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53942754%2fhow-can-i-write-my-own-permissions-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean
and just from row 1 to 8 of the database table
? That example above shows you a template for writing a permission. You define permissions on the models, and then check if that user has the necessary permission using the function? You can apply the permissions to users (the rows 1 - 8, I assume) via the dashboard or via a migration.– Rodney Hawkins
21 hours ago
please check my edit
– Annabelle
20 hours ago
@Annabelle not sure why you wouldn't want to use the Django auth permissions table but if you do want to use a custom permissions table, you can no longer use the usual permissions defined in the model META class because those ones are added to the auth permissions table during migration. You would have to roll out a custom solution. As for your second requirement of row 1-8, it's also a strange one and does not fall into the type of permissions offered by the current permissions framework, but since you will be rolling out a new logic anyway, you can also implement that on your own
– Ken4scholars
20 hours ago
1
@Annabelle What I mean is that is that you can write a DRF permissions class which checks that the user has the permission to change objects of a particular status. The check could be on whether the user is admin or normal user. But then again if you need it to be fine-grained, you can create a separate class for the permissions and define as many as is needed. In essence, what I mean is that you can have them work side by side with the original permissions framework and not replace it
– Ken4scholars
19 hours ago
1
@Annabelle you should import all your models in the
__init__.py
file of the package. Check this out stackoverflow.com/questions/5534206/…– Ken4scholars
19 hours ago