ฉันมีข้อกำหนดด้านล่างนี้เพื่อให้บรรลุโดยใช้ 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