Django Rest Framework passing attributes in url

So, I have two 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 statistics about quality parameters. I have a method that receives some attributes, such as a date range, and aggregates ten statistics for each parameter, and finally returns a json object with all the statistics.

I doubt: can I call this method by passing the parameters that the method needs in the URL and see the results? And, if possible, how can I do this?

Sorry if my explanation is a bit messy, I'm new to Django.

+4
source share
2 answers

GET URL-

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

from rest_framework.views import APIView

class YourView(APIView):


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

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
0

Source: https://habr.com/ru/post/1610600/


All Articles