7

So I have this 2 models:

class ProductQualityMonitor(models.Model):

    product_name = models.CharField(..)
    area = models.CharField(...))
    timeslot = models.DateTimeField(...)

    def get_measured_parameters(self):
        (...)

class QualityParameter(models.Model):
    PARAMETER_CHOICES = (
        (MEAN, MEAN),
        (MEDIAN, MEDIAN),
        (MAXIMUM, MAXIMUM),
        (MINIMUM, MINIMUM),
    )

    name = models.CharField(max_length=50, choices=PARAMETER_CHOICES)
    value = models.FloatField()
    product = models.ForeignKey(ProductQualityMonitor,
                                related_name="parameters")

I need to get some aggregate stats about the quality parameters. I have a method that receives some attributes, like the date range, and aggregates the stats ten by ten for each parameter, and at the end returns a json object with all the aggregate stats.

My doubt is: can I call this method, passing the parameters that the method needs in the url, and see the results? And, if i can, how do I do it?

Sorry if my explanation is a little messy, I am new in Django.

0

3 Answers 3

8

Two ways I did to pass attributes from URL for Django REST framework:

1. From URL path

Sample URL: http://127.0.0.1:8000/myurl/passthisstringfrompath

In urls.py (with the assumption that the attribute is a string):

urlpatterns = [
    path('myurl/<str:myattribute1>/', MyRestApiView.as_view()),
]

In views.py:

class MyRestApiView(APIView):
    def get(self, request, *args, **kwargs):
        if kwargs.get("myattribute1", None) is not None:
            print("the passed attribute:", kwargs["myattribute1"]) # this should print "passthisstringfrompath"
        return Response()
    def post(self):
        pass

The parameter *args and **kwargs are the Python way of passing undefined number of list (*args) and dict (**kwargs) objects, something like Args... in C++, more info here. This is used because in class based views, URL parameters are fetched from this kwargs, more info here.

2. From URL query parameters

Sample URL: http://127.0.0.1:8000/myurl/?myattribute1=passthisstringfromparam

In urls.py:

urlpatterns = [
    path('myurl/', MyRestApiView.as_view()),
]

In views.py:

class MyRestApiView(APIView):
    def get(self, request):
        if request.GET.get("myattribute1", None) is not None:
            print("the passed attribute:", request.GET["myattribute1"]) # this should print "passthisstringfromparam"
        return Response()
    def post(self):
        pass

Or better yet just use "query_params" as advised in documentation for Django under https://www.django-rest-framework.org/api-guide/requests/ by literally substituting all instances of "request.GET" to "request.query_params" and then everything else the same.

Additional info

Of course you can combine both ways above to support URLs that are a combination of both such as: http://127.0.0.1:8000/myurl/passthisstring/?mynextattribute=thenthissecond&mylastattribute=andthenthislast

Where:

  • kwargs["myattribute1"] = "passthisstring"
  • request.query_params["mynextattribute"] = "thenthissecond"
  • request.query_params["mylastattribute"] = "andthenthislast"

Beware of the order of urlpatterns to not mix one with another, always put more specific ones at front of the list:

urlpatterns = [
    path('myurl/<str:myattribute1>/', MyRestApiView.as_view()),
    path('myurl/', MyRestApiView.as_view()),
]

Or better yet, you could also simplify this urlpatterns into a single pattern which takes into account the case when the input attribute is optional:

urlpatterns = [
    re_path('myurl/(?:(?P<myattribute1>\w+)/?)?', MyRestApiView.as_view()),
]

Just beware of the trailing slash / at the end so that the following URLs would lead to the same resource:

Sign up to request clarification or add additional context in comments.

Comments

0

You can pass GET parameters in your URL and retrieve them in your view

your.url.com?param1=value1&param2=value2

Then in your view

from rest_framework.views import APIView

class YourView(APIView):


    def get(self,request):
        parameters = request.query_params
        #use your URL parameters

5 Comments

Sorry by the silly question but... how do i define the url in the url.py? Because url(r'^test/$', views.YourView.as_view()), doesn't work.
you don't need to specify url parameters in your urls.py if you are passing them via GET using ? . Try this url(r'^test/$', views.YourView.as_view({'get':'get'}))
I have this view, like you said: class YourView(APIView): def get(self,request): parameters = request.data return Response(parameters) And the url: url(r'^test/$', views.YourView.as_view()) And then i call: 127.0.0.1:8000/test/?param1=Euro, but the response appears empty. I tried your last suggestion, but gives a error
I solved the problem, i put request.query_params instead of request.data. Thanks for your help!
@SaraTomaz yep, i forgot, for query parameters is query_params. Fixed
0

Pass the parameters in the GET url

Viewsets.py

from rest_framework import status
from rest_framework import viewsets

class YourViewSet(viewsets.ViewSet):
    def get_queryset(self):
        queryset = super(YourViewset, self).get_queryset()
        id = self.request.query_params.get('id', None)
        # do some operations return queryset

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.