[PYTHON] Django: Import a class from a string

Import a class from a Django string.

Here is a sample Repository. Please note that Repository also serves as Setting for each APP.

There are times when you want to import a class from a string in Django. In that case, use import_string ** in ** django.utils.module_loading.

from django.utils.module_loading import import_string

cl = import_string('rest_framework.permissions.IsAdminUser')

use

I set Permission in settings.py as an array of strings and used it when I wanted to use it in the RestFrameWork Viewset.

project/settings.py


SAMPLE_PERMISSIONS = [
    'rest_framework.permissions.IsAuthenticated',
    'sample_app.permissions.SamplePermission'
]

sample_app/views.py


from django.conf import settings
from rest_framework.viewsets import ModelViewSet
from django.utils.module_loading import import_string

from .serializers import SampleSerializer
from .models import Sample


class SampleViewSet(ModelViewSet):
    serializer_class = SampleSerializer
    queryset = Sample.objects.all()
    
    #Used here.
    # IsAuthenticated,SamplePermission applies to Permissions.
    permission_classes = [
        import_string(permission_class) for permission_class in settings.SAMPLE_PERMISSIONS]

You can use it like this. I think there are times when I use it, so for reference,

Here is a sample Repository. Please note that Repository also serves as Setting for each APP.

Well then, have a good Django life.

Recommended Posts

Django: Import a class from a string
Generate a class from a string in Python
Create a pandas Dataframe from a string.
The story of a Django model field disappearing from a class
Python-dynamically call a function from a string
Create an instance of a predefined class from a string in Python
# 5 [python3] Extract characters from a character string
Use Django from a local Python script
How to create a function object from a string
DJango Memo: From the beginning (creating a view)
Create a datetime object from a string in Python (Python 3.3)
Run a Python file from html using Django
Create a Django schedule
Python from or import
Django memo # 1 from scratch
DJango Note: From the beginning (using a generic view)
DJango Note: From the beginning (creating a view from a template)
Steps from installing Python 3 to creating a Django app
Make the model a string on a Django HTML template
unable to import django
Start a Django project
[Django] Give Form a dynamic initial value from Model
Different from the import type of python. from A import B meaning
Build a bulletin board app from scratch with Django. (Part 2)
Build a bulletin board app from scratch with Django. (Part 3)
Python> Read from a multi-line string instead of a file> io.StringIO ()
Django starting from scratch (part: 2)
Django starting from scratch (part: 1)
[Python] Use a string sequence
Create a homepage with django
Is this string a decimal?
[Django] Make a pull-down menu
from sklearn.datasets import fetch_mldata-> fetch_openml
Create a Django login screen
Use django model from interpreter
Template registration from Django Bootstrap
Profile within a class method
Deploy Django + React from scratch to GKE (3) Create a GCP project
Try to extract a character string from an image with Python3
How to get a string from a command line argument in python
Created a class to download and parse XBRL from UFO Catcher
Outputs a line containing the specified character string from a text file
How to extract the desired character string from a line 4 commands
A story about someone who wanted to import django from a python interactive shell and save things to a DB