13

I have to pass the product_id (which is a string) into a view. There I have to do some DB operations based on the product id. How can I get that product id in that view? Actually what should be the parameter in the class ProductDetailConfiguration view? Now I am passing viewsets.ModelViewSet. Actually, this API call is not completely related to any model.

# urls.py

  url(r'^product-configuration/(?P<product_id>[\w-]+)/$', views.ProductDetailConfiguration, name='product-configuration'),

# views.py

class ProductDetailConfiguration(viewsets.ModelViewSet):

    queryset = Product.objects.all()

    def get_queryset(self, **kwargs):
      
        queryset = Product.objects.all()
        product_id = self.request.get('product_id', None)
        #filter query set based on the product_id
        return queryset

    serializer_class = ProductConfigurationSerializer

4 Answers 4

25

The URL parameters are available in self.kwargs.
From the documentation:

Filtering against the URL

Another style of filtering might involve restricting the queryset based on some part of the URL.

For example if your URL config contained an entry like this:

url('^purchases/(?P<username>.+)/$', PurchaseList.as_view()),

You could then write a view that returned a purchase queryset filtered by the username portion of the URL:

class PurchaseList(generics.ListAPIView):
   serializer_class = PurchaseSerializer

   def get_queryset(self):
       """
       This view should return a list of all the purchases for
       the user as determined by the username portion of the URL.
       """
       username = self.kwargs['username']
       return Purchase.objects.filter(purchaser__username=username)
Sign up to request clarification or add additional context in comments.

1 Comment

self.kwargs['param'] -> this is the right answer
2

You have to use lookup_field in your views to get product_id and the view should not belongs to any model. Here I already answer a question like yours using django rest framework to return info by name

Hope this will help to solve your problem.

2 Comments

I have implemented like you suggested in that question. When I print the queryset in view it shows the result. ProductConfigurationCustomSerializer(queryset). Then what should be the model in serializer. When I commented the model references it throws error.
Actually, what do you want to do? In your question, you just asked for getting product_id in views.
0

This is my approach:

Update URL:

url(r'^product-configuration$', views.ProductDetailConfiguration, name='product-configuration'),

In view.py:

class ProductDetailConfiguration(viewsets.ModelViewSet):
    lookup_field = 'product_id'
    serializer_class = ProductConfigurationSerializer

    def retrieve(request, product_id=None, *args, **kwargs):
        queryset = self.get_queryset()
        # Filter with produc_id
        # print(product_id)
        # Return Response

Comments

-1

You can get the data from the request url by :- data_dict = self.request.data This will return a dictionary of all the parameters of the request url. You can use the parameter name as the key

1 Comment

URL parameters are not in there.

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.