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
orderid = request.data['id] point = request.data['point_id'], jsonorder = request.data. And remember that JSONField is postgres thing and will not work with default django sqlite DB