(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs)
Create 'view' permissions for all models.

    ``django.contrib.auth`` only creates add, change and delete permissions.
    Since we want to support read-only views, we need to add our own
    permission.

    Copied from ``https://github.com/django/django/blob/1.9.6/django/contrib/auth/management/__init__.py#L60``.
Create 'view' permissions for all models.
Create view permissions for all models .
def create_view_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs):  # noqa
    """
    Create 'view' permissions for all models.

    ``django.contrib.auth`` only creates add, change and delete permissions.
    Since we want to support read-only views, we need to add our own
    permission.

    Copied from ``https://github.com/django/django/blob/1.9.6/django/contrib/auth/management/__init__.py#L60``.

    """
    if not app_config.models_module:
        return

    try:
        Permission = apps.get_model('auth', 'Permission')
    except LookupError:
        return

    if not router.allow_migrate_model(using, Permission):
        return

    from django.contrib.contenttypes.models import ContentType

    # This will hold the permissions we're looking for as
    # (content_type, (codename, name))
    searched_perms = list()
    # The codenames and ctypes that should exist.
    ctypes = set()
    for klass in app_config.get_models():
        # Force looking up the content types in the current database
        # before creating foreign keys to them.
        ctype = ContentType.objects.db_manager(using).get_for_model(klass)

        ctypes.add(ctype)
        perm = (get_permission_codename('view', klass._meta), 'Can view %s' % (klass._meta.verbose_name_raw))
        searched_perms.append((ctype, perm))

    # Find all the Permissions that have a content_type for a model we're
    # looking for.  We don't need to check for codenames since we already have
    # a list of the ones we're going to create.
    all_perms = set(Permission.objects.using(using).filter(
        content_type__in=ctypes,
    ).values_list(
        "content_type", "codename"
    ))

    perms = [
        Permission(codename=codename, name=name, content_type=ct)
        for ct, (codename, name) in searched_perms
        if (ct.pk, codename) not in all_perms
    ]
    # Validate the permissions before bulk_creation to avoid cryptic
    # database error when the verbose_name is longer than 50 characters
    permission_name_max_length = Permission._meta.get_field('name').max_length
    verbose_name_max_length = permission_name_max_length - 11  # len('Can change ') prefix
    for perm in perms:
        if len(perm.name) > permission_name_max_length:
            raise ValidationError(
                "The verbose_name of %s.%s is longer than %s characters" % (
                    perm.content_type.app_label,
                    perm.content_type.model,
                    verbose_name_max_length,
                )
            )
    Permission.objects.using(using).bulk_create(perms)
    if verbosity >= 2:
        for perm in perms:
            print("Adding permission '%s'" % perm)
def create_view_permissions ( app_config , verbosity = 2 , interactive = True , using = DEFAULT_DB_ALIAS , * * kwargs ) : # noqa if not app_config . models_module : return try : Permission = apps . get_model ( 'auth' , 'Permission' ) except LookupError : return if not router . allow_migrate_model ( using , Permission ) : return from django . contrib . contenttypes . models import ContentType # This will hold the permissions we're looking for as # (content_type, (codename, name)) searched_perms = list ( ) # The codenames and ctypes that should exist. ctypes = set ( ) for klass in app_config . get_models ( ) : # Force looking up the content types in the current database # before creating foreign keys to them. ctype = ContentType . objects . db_manager ( using ) . get_for_model ( klass ) ctypes . add ( ctype ) perm = ( get_permission_codename ( 'view' , klass . _meta ) , 'Can view %s' % ( klass . _meta . verbose_name_raw ) ) searched_perms . append ( ( ctype , perm ) ) # Find all the Permissions that have a content_type for a model we're # looking for.  We don't need to check for codenames since we already have # a list of the ones we're going to create. all_perms = set ( Permission . objects . using ( using ) . filter ( content_type__in = ctypes , ) . values_list ( "content_type" , "codename" ) ) perms = [ Permission ( codename = codename , name = name , content_type = ct ) for ct , ( codename , name ) in searched_perms if ( ct . pk , codename ) not in all_perms ] # Validate the permissions before bulk_creation to avoid cryptic # database error when the verbose_name is longer than 50 characters permission_name_max_length = Permission . _meta . get_field ( 'name' ) . max_length verbose_name_max_length = permission_name_max_length - 11 # len('Can change ') prefix for perm in perms : if len ( perm . name ) > permission_name_max_length : raise ValidationError ( "The verbose_name of %s.%s is longer than %s characters" % ( perm . content_type . app_label , perm . content_type . model , verbose_name_max_length , ) ) Permission . objects . using ( using ) . bulk_create ( perms ) if verbosity >= 2 : for perm in perms : print ( "Adding permission '%s'" % perm )