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')
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