0

I want to capture url values from a url into my views in a post request.

My urls.py looks like below

from django.urls import path from . import views as projects_views

urlpatterns = [
    path('<str:project_id>/comments', projects_views.ProjectCommentList.as_view(), name="project_comments"),
]

and I want to capture the value project_id in my ListCreateAPIView. Get call works fine.

My views.py looks like below

class ProjectCommentList(generics.ListCreateAPIView):
    queryset = projects_models.Comment.objects.all()
    serializer_class = projects_serializers.CommentSerializer


    def get(self, request, project_id=None):
        queryset = self.filter_queryset(self.get_queryset())
        queryset = queryset.filter(project__id=project_id)

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)

    def perform_create(self, serializer):
        project_id = ###  Need "project_id" here
        project = projects_models.Project.objects.get(id=data.get('project_id'))
        serializer.save(project=project)

How can this be done?

2
  • Write a post method like you did for get? Commented Aug 22, 2019 at 4:44
  • No, it still doesn't capture the value :( Commented Aug 22, 2019 at 4:48

3 Answers 3

1

Use self.kwargs["project_id"] to get the project_id in perform_create() method,

class ProjectCommentList(generics.ListCreateAPIView):
    ...

    def perform_create(self, serializer):
        project_id = self.kwargs["project_id"]
        project = projects_models.Project.objects.get(id=data.get('project_id'))
        serializer.save(project=project)
Sign up to request clarification or add additional context in comments.

4 Comments

also, the request url?
i haven't separately declared the post method, i am using the same provided with ListCreateApiView, my request url is of the format /123/comments where i create a comment for the post with id 123.
whats the result of print("self.kwargs")?
For me if is replace (self, serializer) from the parameters of perform_create to (self, *args, **kwargs) I get the received kwargs as {} but self.kwargs has the value I was looking for, thanks a lot
1

Create a scope varibale in your get method for the same class using "self"

def get(self, request, project_id=None):
        ...
        self.project_id = project_id
        ...

    def perform_create(self, serializer):
        ...
        project_id = self.project_id OR q = ExampleQuery.filter(project_id=self.project_id)
        ...

3 Comments

i'm not sure if it'll work, as when we're doing a post call I don't think the get will be called so the self.project_id will not be set.
If you are using a post call, then you can add self.project_id = project_id in post() as well. I am assuming, you are just using get()
No, my issue was where I can get the project_id from my url, I got the answer, its there in the self.kwargs . I am using both post and get.
1

self.kwargs contains the all keyword arguments provided in url path. so, we can access the project_id like self.kwargs['project_id']. Use a print statement like print(self.kwargs) to know what actually self.kwargs has.

def perform_create(self, serializer):
    # ... your code
    project_id = self.kwargs['project_id']
    # ... your code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.