Why is global config inside settings.py is not used? #9108
Unanswered
hantsaniala
asked this question in
Question & Answer
Replies: 2 comments 2 replies
-
can you please more specific on which are not working or need to be done twice? |
Beta Was this translation helpful? Give feedback.
1 reply
-
if you're encountering repetitive configurations and looking to follow the DRY (Don't Repeat Yourself) principle, you can create a custom base viewset class with these common configurations: from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from rest_framework_simplejwt.authentication import JWTAuthentication
from core.utils import StdPagination # Make sure to use the correct import path for your StdPagination
class BaseViewSet(viewsets.ModelViewSet):
"""
This is a custom base viewset class designed to minimize repetition.
It encapsulates common settings such as authentication, permissions, and pagination.
By inheriting from this base viewset in your specific viewsets, you eliminate
the need to manually specify these attributes for each view.
"""
permission_classes = [IsAuthenticated]
authentication_classes = [JWTAuthentication]
pagination_class = StdPagination
class YourViewSet(BaseViewSet):
"""
This is a specific viewset for your model, inheriting from the BaseViewSet.
Here, you only need to define model-specific attributes like 'queryset' and 'serializer_class'.
This approach adheres to the DRY principle and maintains a consistent configuration
across your views.
"""
queryset = YourModel.objects.all()
serializer_class = YourModelSerializer # Replace with your serializer class By adopting this approach, you make your code cleaner, more maintainable, and less error. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone,
I can't figure out why all values inside settings.py is not used, but instead I must specify all values manually for every ModelViewSet. What I am missing?
Here is my settings.py
It only works when I specify all config manually for every view.
My requirements.txt
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions