Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Add New Viewset (UsefulViewSet) in DRF #9610

Closed
wants to merge 1 commit into from

Conversation

shihab2004
Copy link

@shihab2004 shihab2004 commented Dec 21, 2024

UsefulViewSet is an updated version of ModelViewSet. It allows more flexibility to control ModelViewSet. Additionally, it supports individual templates & serializers for individual views.

Code Example


class BookingViewSet(AdminBaseViewSet):
    queryset = Booking.objects.all().order_by('-pk')
    serializer_class = BookingSerializer
    retrieve_serializer = BookingRetrieveSerializer
    update_serializer = BookingUpdateSerializer

    template_name = "booking/list.html"
    update_template = 'booking/view.html'
    
    def get_list_extra_context(self, request, *args, **kwargs):
        # Your list view code.
        return {
           # Your list view extra data
        }
    
    def get_retrieve_extra_context(self, request, *args, **kwargs):
        # Your retrieve view code.
        return {
           # Your retrieve view extra data 
        }

UsefulViewSet is an updated version of ModelViewSet. It allows more flexibility to control ModelViewSet. Additionally, it supports individual templates & serializers for individual views.
@onegreyonewhite
Copy link
Contributor

Maybe I’ll express an unpopular opinion, but the idea isn’t bad. The implementation, though, could use some work.

I did something similar in the vstutils project. There, you can override the serializer for each action.

As for templates and context, I can’t comment since I don’t use them in APIs.

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be adding this to https://github.com/chibisov/drf-extensions as AdminViewSet would make more sense?

@tomchristie
Copy link
Member

Yep, what @auvipy said.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants