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

How to replace the default implementation of some methods #517

Open
direcyoung opened this issue Dec 28, 2024 · 1 comment
Open

How to replace the default implementation of some methods #517

direcyoung opened this issue Dec 28, 2024 · 1 comment
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@direcyoung
Copy link

For example, if I want to replace the post method, I need to modify the access path to group/post/ instead of using the group/

class GroupViewSet(viewsets.APIViewSet):
    router = router
    model = Group
    default_request_body = GroupIn
    default_response_body = GroupOut
        
    list_groups = views.ListView()
    read_groups = views.ReadView()
    delete_groups = views.DeleteView()


@router.post("post/", response={200: GroupOut, 400: Error})
def xxx
@hbakri
Copy link
Owner

hbakri commented Jan 13, 2025

Hi @direcyoung, thanks for raising this! ✨
Let me help clarify how paths work in django-ninja-crud:

  1. The basic route structure works like this:
class GroupViewSet(APIViewSet):
    model = Group
    
    create_group = CreateView(...)  # POST /
    list_groups = ListView(...)     # GET /
    read_group = ReadView(...)      # GET /{id}
    update_group = UpdateView(...)  # PUT /{id}
    delete_group = DeleteView(...)  # DELETE /{id}
  1. The /group prefix should be defined when registering the router:
api.add_router("/group", group_router)
  1. If you want to override the create endpoint, you can simply remove it from the viewset and define your custom method with the root path /:
@router.post("/", response={200: GroupOut, 400: Error})
def create_group(self, request):
    # your custom logic here

You can find more details in the CreateView documentation. Let me know if you need any clarification! 🤗

@hbakri hbakri self-assigned this Jan 13, 2025
@hbakri hbakri added documentation Improvements or additions to documentation question Further information is requested labels Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants