4

I want to create the permissions in views dynamically instead of the defaults at models

I create a main class called CreatorView

from django.views.generic import View
from django.forms.models import modelform_factory
class CreatorView(View):
    model = None
    fields = None
    exclude = None
    form = None
    page_title = ''
    
    def create_form(self):
        default =dict()
        if self.fields == None:
            if self.exclude == None:
                default['fields'] = self.fields = self.model._meta.fileds
            else:
                default['exclude'] = self.exclude
        else:
            if self.exclude:
                raise Exception('error')
            default['fields'] = self.fields
             

        return modelform_factory(self.model,**default)
    def get(self,request,*args,**kwargs):
        return render('','base.html')
    def post(self,request,*args,**kwargs):
      ... and so on

the main urls is:

urlpatterns = [
path('%s/%s' % (cls.model._meta.app_label, cls.__name__.lower()), cls.as_view(),
     name='%s/%s' % (cls.model._meta.app_label, cls.__name__.lower())) for cls
in CreatorView.__subclasses__()]

if I make inheritance from CreatorView then my class should create a page for example:

class Login(CreatorView):
    model = Users
    """ my overwrite methods and actions """

class Configurations(CreatorView):
    model = Configure
    """ my overwrite methods and actions """

class Teachers(CreatorView):
    model = Teachers
    """ my overwrite methods and actions """

class Students(CreatorView):
    model = Students
    """ my overwrite methods and actions """

and so on

this code will create to me four pages I want to create table semi to django content_type model to be like:

id app_label page
1 myapp login
2 myapp configurations
3 myapp teachers
4 myapp students

Can I modify auth_permission table in Django to make content_type foreign key from my content_type? If I can how to prevent insert default permissions and make my insertion for default permission?

AlASAD WAIL
  • 775
  • 4
  • 18

1 Answers1

3

You can create Permissions manually according to docs here

from myapp.models import BlogPost
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType

content_type = ContentType.objects.get_for_model(BlogPost)
permission = Permission.objects.create(
    codename='can_publish',
    name='Can Publish Posts',
    content_type=content_type,
)

OR

An enhanced permission library which enables a logic-based permission system to handle complex permissions in Django. here

Lord-shiv
  • 1,103
  • 2
  • 9
  • 19