简体   繁体   中英

Django - How to work with `auth_permission` Model

Django make its own permission models, like auth_permission .
I want to add new permission doing this by code.

How I normally add something to a table

from .models import Model

mdl, succeeded = Model.objects.get_or_create(
    field=value,
    field=value
)
mdl.save()

But to call the auth_permission Model, I have to import the model first.
Only I can't find how this Model is called.

So do one of you guys know what to import to call this Model?

You can import the permission model with

from django.contrib.auth.models import Permission

However, if you want to create custom permissions, the documented approach is to add permissions to a model. Django will then create the permissions when you run manage.py migrate .

class MyModel(models.Model):
    ...
    class Meta:
        permissions = (
            ("can_do_something", "Can do something"),
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM