I created a DRF API endpoint in order to be able to grab some data to my database and show it on my Django page using Jquery.
My sample data looks like this:
{
"item": "Someitem",
"Price": 120,
"Status": "Free"
},
{
"item": "SecondItem,
"Price": 90,
"Status": "Taken"
},
So if i retrieve the endpoint from JQuery to this link: http://127.0.0.1:8000/tst/, i'll get all the records and have all of them shown in my web page. But what if, for example, i only want to retrieve only the records whose Status field is set to Taken? Is there any way to edit the DRF request so that it points to http://127.0.0.1:8000/tst/Taken? Or instead, if the user wants to retrieve all the others with the status set to Free, it will point to http://127.0.0.1:8000/tst/Free? I know i could do it with jquery, but i would actually like to do it server-side.
I tried with this:
queryset = tst.objects.filter(Status="Taken")
But the problem here, is that it will always take only the Taken records from my DB. In my case, i want to find a way to retrieve Taken sometimes, and Free some other times from the template.
I'm fairly new to DRF, so my setup is pretty basic:
views.py
class tstList(generics.ListCreateAPIView):
queryset = tst.objects.all()
serializer_class = tstSerializer
class tstDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = tst.objects.all()
serializer_class = tstSerializer
url.py
path('tst/', views.tstList.as_view()),
path('tst/<int:pk>/', views.tstDetail.as_view()),
models.py
class tst(models.Model):
item = models.CharField()
Price = models.FloatField()
Status = models.CharField()
def save(self, *args, using=None, **kwargs):
super(tst, self).save(*args, **kwargs)
from Filter json records in Django Rest Framework
No comments:
Post a Comment