What's the best way to have app access permissions?

35 views
Skip to first unread message

Tal

unread,
Jul 16, 2019, 12:25:13 PM7/16/19
to Django users
I know you can have permissions on individual models that control which users have access to them, but I need something for my apps.

Ex. If a user logs in, does he have access to the store_app? The forum_app? etc.

I have a solution that looks something like this:

models.py:

class MyUser(AbstractUser):
   
for app_short_name, app_long_name in settings.APP_CHOICES:
        add_field
= "allow_{} = ".format(app_short_name)
        add_field
+= "models.BooleanField(verbose_name='Allow Access to {}', default=False, null=False)".format(app_long_name)

       
exec(add_field)


settings.py:

APP_CHOICES = [
   
('store_app', 'Store App'),
   
('forum_app', 'Forum App'),
]

It works, but I'm not sure if it's bad practice to have dynamically generated fields in models.py like this, or if there's a better/simpler way to do this.

Jani Tiainen

unread,
Jul 16, 2019, 1:30:16 PM7/16/19
to django...@googlegroups.com
We use django groups for that. So we give groupnames like ALLOW_SOMETHING and then in code and templates we do check if user has that group or not.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e2672373-5cc1-495d-8d46-5e99bdf58fbe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages