0
votes

I am creating my first Django project . I have taken 30,000 values as input and want to show particular values according to primary key .

Code:

class employeesList(APIView):

def get(self,request):
    employees1 = employees.objects.all()

    with open('tracking_ids.csv') as f:
        reader = csv.reader(f)
        for row in reader:
            _, created = employees.objects.get_or_create(
            AWB=row[0],
            Pickup_Pincode=row[1],
            Drop_Pincode=row[2],
            )
    serializer = employeesSerializer(employees1 , many=True)
    return Response(serializer.data)

def post(self,request):
    # employees1 = employees.objects.get(id=request.employees.AWB)
    employees1 = employees.objects.all()
    serializer = employeesSerializer(employees1 , many=True)
    return Response(serializer.data)        

If I enter http://127.0.0.1:8000/employees/ in URL , I get all the values . I want the URL to be like http://127.0.0.1:8000/employees/P01001168074 and show values of P01001168074 where P01001168074 is primary ID .

I have read 1:showing the model values of specific user django 2)editing user details in python django rest framework but they are different

Can it be done and if it can , then how ?

2

2 Answers

2
votes

Presuming that you are using Django 2.0 you must configure a path than can capture your parameter as seen in the documentation

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.serializers import ModelSerializer

class A(models.Model):
    x = models.CharField(max_length=250)


class MySerializer(ModelSerializer):
    class Meta:
        model = A
        fields = ('x',)


class MyListView(APIView):

    def get(self, request, *args, **kwargs):
        # simply get all the objects, serialize and return the response
        elems = A.objects.all()
        response_data = MySerializer(elems, many=True).data
        return Response(data=response_data)


class MyDetailView(APIView):

    def get(self, request, *args, **kwargs):
        # we obtain the parameter from the URL
        desired_item = kwargs.get("desired_item", None)

        # we filter the objects by it and get our instance
        element = A.objects.filter(x=desired_item).first()

        # serialize the instance and return the response
        response_data = MySerializer(element).data
        return Response(data=response_data)

# all we have to now is define the paths for the list and the detail views.
urlpatterns = [
    path('employees/', MyListView.as_view()),
    path('employees/<str:desired_item>', MyDetailView.as_view())
]
1
votes

One good option is to use a viewset that already includes a list and detail endpoint and requires little to code for a default simple setup

views.py

from rest_framework import viewsets


class EmployeeViewSet(viewsets.ModelViewSet):
    serializer_class = EmployeeSerializer
    queryset = Employee.objects.all()

urls.py

from rest_framework.routers import SimpleRouter
from views import EmployeeViewSet


router = SimpleRouter()
router.register(r'employees', EmployeeViewSet, base_name='employees')

urlpatterns = router.get_urls()

You can read more about viewsets in the DRF docs