Django Rest Api - รับและโพสต์คำขอในครั้งเดียว - โพสต์คำขอตามผลลัพธ์ที่ได้รับ

ฉันมีข้อกำหนดด้านล่างนี้เพื่อให้บรรลุโดยใช้ Django Rest Framework ฉันต้องจัดการคำขอ POST ไปยังรุ่น 2 ภายในคำขอรับของรุ่น 3

ฉันมีสองรุ่น เก็บไว้เพียงไม่กี่คอลัมน์เท่านั้น

รุ่น.py

class Customers(models.Model):  #original customers data are stored here 
    customer_id = models.BigIntegerField(db_column='customer_id', primary_key=True)  
    customer_name = models.CharField(db_column='Customer_name', blank=True, null=True, max_length=50)  

class Customers_In_Use(models.Model):  #where we will track the locking of customers  data once they get used
    customer_id = models.OneToOneField(Customers_Master, to_field='customer_id', on_delete=models.DO_NOTHING, related_name='rel_customer')  
    comments = models.TextField(blank=True,null=True)

มุมมองฐานข้อมูลเดียว

Customers_View(models.Model): 
    customer_id = models.BigIntegerField()
    customer_name = models.CharField()
    in_use = models.CharField(blank=True)

    class Meta:
        managed = False

มุมมองนี้สร้างขึ้นที่แบ็กเอนด์ดังต่อไปนี้

Select 
 C.customer_id, 
 C.customer_name, 
 CASE WHEN U.customer_id_id IS NULL THEN 'No' ELSE 'Yes' END AS In_Use,
from Customers C
left join
Customers_In_Use U
on C.customer_id=U.customer_id_id

ใน Django Rest Api ของฉัน ฉันกำลังเปิดเผยข้อมูลตาม Customers_View (คำขอ GET)

ฉันมีคำขอ POST ถึง Customers_In_Use ซึ่งจะได้รับ customer_id และความคิดเห็นในรูปแบบ json

Example Data on Customers_View:
customer_id,Customer_name,in_use
123,John,No
456,Smith,No
789,John,No
987,Tom,Yes  #Yes means this customer data is already in use 
567,Tom,No

ตอนนี้บน api ถ้าฉันเรียกใช้คำขอนี้

127.0.0.1:8000/api/customers_view/?in_use=No&customer_name=Tom

ฉันควรได้ผลลัพธ์ดังนี้

{
customer_id:567,
customer_name=Tom,
in_use:No
}

เนื่องจากฉันได้รับ customer_id 567 ฉันจึงต้องส่งคำขอโพสต์ไปที่ Customers_In_Use

Typical post request format with below data to be passed
{
comment:'I will use this data',
customer_id:567
}

ตอนนี้คำถามของฉันคือสามารถทำได้ในคราวเดียวหรือไม่?

ทันทีที่มีการเรียกคำขอ GET บน customer_view เราควรส่งคำขอโพสต์ไปที่ Customers_In_Use

จากด้านสงบ:

ฉันได้เขียน listview ง่ายๆจาก customer_view

class customers_views_List(generics.ListAPIView):
    queryset = customers_view.objects.all()
    serializer_class = customers_views_serializer

บน customer_in_use ฉันใช้ viewset แล้ว

class customers_In_Use_api(viewsets.ModelViewSet):
    lookup_field = 'customer_id'  
    queryset = customers_in_use.objects.all()  
    serializer_class = customers_in_use_Serializer 

person just10minutes    schedule 08.03.2018    source แหล่งที่มา


คำตอบ (1)


ใช่คุณสามารถ,

เพียงแทนที่วิธีการสร้างของคลาสของคุณ customer_In_Use_api

คุณต้องใส่ความคิดเห็น customer_name และ in_use ไว้ในคำขอเนื้อหา (หากคุณใช้ jquery ajax ให้ใส่ไว้ในช่องข้อมูลเป็นต้น)

คุณเพียงแค่ต้องรับรหัสของ Customers_View ของคุณจากฟิลด์ in_use และชื่อ

หลังจากนั้นเพียงบันทึก Customer_In_Use ใหม่ของคุณ

ตัวอย่างของฟังก์ชัน create :

def create(self, request):
    in_use = request.data.get('in_use')
    name = request.data.get('customer_name')
    comment = request.data.get('comment')
    customers_view = get_object_or_404(Customers_View, customer_name=name)
    if customers_view.in_use != in_use:
        return Response({'error': 'Not found'}, status=status.HTTP_404_NOT_FOUND)
    serializer_datas['customer_id'] = customers_view.id
    serializer_datas['comment'] = comment
    serializer = customers_in_use_Serializer(serializer_datas)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response({'error': 'Not found'}, status=status.HTTP_404_NOT_FOUND)

อย่าลืมการนำเข้าทั้งหมดเช่นสถานะและ get_object_or_404 และควรจะใช้งานได้ รหัสนี้ไม่ได้รับการทดสอบ ดังนั้นคุณอาจต้องแก้ไขข้อผิดพลาดบางประการ

person greg95000    schedule 08.03.2018
comment
ขอบคุณมากสำหรับคำแนะนำของคุณ ขอเวลาฉันหน่อย ฉันจะลองอัปเดตดูว่าจะเกิดอะไรขึ้น - person just10minutes; 09.03.2018
comment
ฉันเห็นคุณพูดถึงสิ่งนี้ คุณเพียงแค่ต้องรับรหัสของ Customers_View ของคุณจากฟิลด์ in_use และชื่อ แต่ชื่อลูกค้าของฉันอาจไม่ซ้ำกัน ทำการร้องขอรับวิธีการสร้างทริกเกอร์ customer_views_List บน customer_In_Use_api ด้วยหรือไม่ - person just10minutes; 12.03.2018
comment
ดังนั้นผลลัพธ์ที่ต้องการของ 127.0.0.1:8000/api/customers_view/?in_use=No&customer_name=John คืออะไร? - person greg95000; 13.03.2018
comment
มันควรจะเป็น { customer_id:123, customer_name=John, in_use:No } แต่เมื่อฉันเรียก URL เดียวกันอีกครั้ง ฉันควรได้รับ { customer_id:789, customer_name=John, in_use:No } เพราะแอตทริบิวต์ลูกค้า 123 in_use ควรมีการเปลี่ยนแปลง เป็น 'ใช่' ซึ่งเป็นส่วนหนึ่งของการเรียก URL ครั้งแรก หากฉันพึ่งพา Customer_name และ In_use เพื่อเปลี่ยนข้อมูลในรูปแบบ Customers_In_Use ฉันก็จะตอบ 'ใช่' กับลูกค้าทั้งสองในคำขอเริ่มต้นของฉัน - person just10minutes; 14.03.2018