Django Rest Api-一站式获取和发布请求-基于获取结果的发布请求 [英] Django Rest Api - Get and Post Request in One Go - Post Request based on the Get Result

查看:37
本文介绍了Django Rest Api-一站式获取和发布请求-基于获取结果的发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用Django Rest Framework达到以下要求.我需要在模型3的Get请求中处理对模型2的POST请求

I have this below requirement to achive using Django Rest Framework. I need to handle POST request to Model 2 within the Get request of Model 3

我有两个模型,只保留了几列

I have two models, Kept only few columns

models.py

models.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

此视图在后端构建,如下所示

This view is built at backend as below

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请求)公开数据

On my Django Rest Api I am exposing data based on Customers_View (GET request)

我对Customer_In_Use有一个POST请求,该请求将以json格式接收customer_id和注释.

I have a POST request to Customers_In_Use which will receive customer_id and comments in a json format.

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

如果我运行此get请求,请立即在api上

now on api if i run this get request

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

Since i got the customer_id 567 I need to send a post request to Customers_In_Use

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

现在我的问题是可以一次完成吗?

Now my question is can this be done at one go?

在customers_view上调用GET请求后,我们应将发帖请求发送到Customer_In_Use

The moment a GET request is called on customers_view we should send post request to Customers_In_Use

从安宁的角度来看:

我已经编写了来自customer_view的简单列表视图

I have written simple listview fro customer_view

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

在customers_in_use上,我已经使用了视图集

on customers_in_use i have used viewset

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

推荐答案

是的,

只需覆盖您对customer_In_Use_api类进行创建的方法.

Just override the create method of you class customers_In_Use_api.

您还必须将注释,customer_name和in_use放在正文请求中(例如,如果您使用的是jquery ajax,则将其放在数据字段中)

You also have to put the comment, customer_name and in_use inside the body request (if you're using jquery ajax just put it in the data fields for example)

您只需要通过in_use和name字段获取Customer_View的ID.

You juste have to get the id of your Customers_View by the in_use and name fields.

之后,只需保存新的Customer_In_Use

After that just save your new Customer_In_Use

创建函数的示例:

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这样的所有导入,它应该可以工作.该代码未经测试,因此也许您必须纠正一些错误

Don't forget all the imports like the status and the get_object_or_404 and it should work. This code is not tested so maybe you'll have to correct some mistakes

这篇关于Django Rest Api-一站式获取和发布请求-基于获取结果的发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆