How can I use Django permissions without defining a content type or model?

后端 未结 6 1104
小鲜肉
小鲜肉 2021-01-29 18:56

I\'d like to use a permissions based system to restrict certain actions within my Django application. These actions need not be related to a particular model (e.g. access to sec

6条回答
  •  滥情空心
    2021-01-29 19:26

    Django's Permission model requires a ContentType instance.

    I think one way around it is creating a dummy ContentType that isn't related to any model (the app_label and model fields can be set to any string value).

    If you want it all clean and nice, you can create a Permission proxy model that handles all the ugly details of the dummy ContentType and creates "modelless" permission instances. You can also add a custom manager that filters out all Permission instances related to real models.

提交回复
热议问题