# Add Custom Permissions to Views without Defining Models

**URL:** <https://forum.djangoproject.com/t/add-custom-permissions-to-views-without-defining-models/18960>\
**Category:** Getting Started\
**Created:** [February 20, 2023, 3:05am UTC](https://forum.djangoproject.com/t/add-custom-permissions-to-views-without-defining-models/18960 "2023-02-20T03:05:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lamarche-Lam](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lamarche-lam/32/6509_2.png) [@Lamarche-Lam](https://forum.djangoproject.com/u/Lamarche-Lam)\
**Post date:** [February 20, 2023, 3:05am UTC](https://forum.djangoproject.com/t/add-custom-permissions-to-views-without-defining-models/18960/1 "2023-02-20T03:05:56Z")

</div>

Hello everyone,

I created an app to support front-end custom query directly to an external database which didn’t have any Django-related tables (something like django\_contenttype, django\_session.)Similar to other apps, this app should be limited to a group of people to use. Therefore, I need a proper permission control policy in active.

> However, the external database didn’t have any Django-related tables since I can’t define models for the app and make migration to the external database due to company policy, which means, I can’t simply use the permissions that come with defining the models (VIEW, CHANGE, DELETE or anything custom in the META part of the model class).

Since the app is just for query, nothing DDL happening here, do anyone know is there a practical way to add custom permission to certain view without defining a model to do so?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 20, 2023, 3:43am UTC](https://forum.djangoproject.com/t/add-custom-permissions-to-views-without-defining-models/18960/2 "2023-02-20T03:43:37Z")

</div>

You’re still going to want a local database for Django itself to use. That’s where the permissions are stored, not in the user models.

The permission system will still work as normal with the data stored in an external database.

---

<div class="post-metadata">

**Author:** ![Lamarche-Lam](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lamarche-lam/32/6509_2.png) [@Lamarche-Lam](https://forum.djangoproject.com/u/Lamarche-Lam)\
**Post date:** [February 20, 2023, 5:38am UTC](https://forum.djangoproject.com/t/add-custom-permissions-to-views-without-defining-models/18960/3 "2023-02-20T05:38:45Z")

</div>

I do have a local database for Django itself to use, but the data to be queried is stored in another database. Sorry I did’t make myself clear about the setting of the database. Here is how the setting looks like:

```auto
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME' : '',
        'USER' : '',
        'PASSWORD' : '',
        'HOST' : '127.0.0.1',
        'PORT' : 3306,
        'OPTIONS':{
            'autocommit' : True
        }
    },
    'query_1104_db': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME' : '',
        'USER' : '',
        'PASSWORD' : '',
        'HOST' : '127.0.0.1',
        'PORT' : 3306,
        'OPTIONS':{
            'autocommit' : True
        }
    }
}

```

The query\_1104\_db is the “external” database. I created a view under the app, “data\_query”, to perform raw SQL query to this database using django.db.connections.cursor(). By saying

> That’s where the permissions are stored, not in the user models.

do you mean I need a third database for storing all the custom permissions?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 20, 2023, 11:50am UTC](https://forum.djangoproject.com/t/add-custom-permissions-to-views-without-defining-models/18960/4 "2023-02-20T11:50:19Z")

</div>

No, the permissions table is in your default database.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 20, 2023, 1:15pm UTC](https://forum.djangoproject.com/t/add-custom-permissions-to-views-without-defining-models/18960/5 "2023-02-20T13:15:37Z")

</div>

Side note:

> [@Lamarche-Lam](#):
>
> I created a view under the app, “data\_query”, to perform raw SQL query to this database using django.db.connections.cursor().

You don’t need to do it that way. You can create models for the external database and use the ORM to perform your queries.
