简体   繁体   中英

How to get data from django rest framework request field

I have 2 models: Printer and Check

models.py

class Printer(models.Model):
    name        = models.CharField(...)
    api_key     = models.CharField(...)
    check_type  = models.CharField(...)                                    
    point_id    = models.IntegerField()

class Check(models.Model):
    printer_id      = models.ForeignKey(Printer, on_delete=models.CASCADE)
    type            = models.CharField(...)
    order           = JSONField(...)
    status          = models.CharField(...)
    pdf_file        = models.FileField(...)

I am building API using Django REST Framework. And I am getting POST request that should look this way:

request "/create_checks/"

{
  "id": 123456,
  "price": 780,
  "items": [
    {
      "name": "pizza",
      "quantity": 2,
      "unit_price": 250
    },
    {
      "name": "rolls",
      "quantity": 1,
      "unit_price": 280
    }
  ],
  "address": "some address",
  "client": {
    "name": "John",
    "phone": his phone
  },
  "point_id": 1
}

Every point(place where food is cooking) has two printers. I need to create two Check objects so one "check" prints for kitchen and one for client. For that I am going to use "point_id" from request and create two Check

views.py

@api_view(['POST'])
def create_checks(request):
    queryset = Check.objects.all()
    orderid = #order_id from request
    point = #point_id from request
    jsonorder = #request body converted to json
    printers = Printer.objects.filter(point_id=point)
    kitchencheck = Check(printer_id=Printer.objects.get(name=printers[0].name), 
                        type="kitchen", order=jsonorder,status="new")
    clientcheck = Check(printer_id=Printer.objects.get(name=printers[1].name), 
                        type="client", order=jsonorder,status="new")
    kitchencheck.save()
    clientcheck.save()
    return Response({"Success": "Checks created successfully"}, status=status.HTTP_200_OK)

1. How do I get order_id and point_id from request?
2. How can I conver request body to JSON file?
3. Is there easier way to do it? I've spent whole day trying to understand DRF and "result" looks too bulky and unrealistic

This is what serializers are for. ( https://www.django-rest-framework.org/api-guide/serializers/ ) At the View-level, pass your request through a serializer, which will give you a validated_data payload. This in turn you can use to create model instances. Just repeat the last step twice if you need to create two model from one set of data.

class CheckSerializer(serializers.Serializer):
    id = serializers.CharField()
    point_id = serializers.CharField()
    # etc ..

@api_view(['POST'])
def create_checks(request):
    queryset = Check.objects.all()

    serializer = CheckSerializer(data=request.data,
                                context={'request': request})
    serializer.is_valid(raise_exception=True)

    printers = Printer.objects.filter(point_id=serializer.validated_data['point_id'])

    kitchencheck = Check(
        printer_id=Printer.objects.get(name=printers[0].name), 
        type="kitchen",
        order=jsonorder,
        status="new"
        )

    clientcheck = Check(
        printer_id=Printer.objects.get(name=printers[1].name), 
        type="client",
        order=jsonorder,
        status="new"
        )

    kitchencheck.save()
    clientcheck.save()
    return Response({"Success": "Checks created successfully"}, status=status.HTTP_200_OK)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM