Django Rest Api - Get and Post Request in One Go -

2019-08-23 16:37发布

问题:

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

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)

one database view.

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

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

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

now on api if i run this get request

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

I should get result as below

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

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?

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

From restful side:

I have written simple listview fro customer_view

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

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 

回答1:

Yes you can,

Just override the create method of you class customers_In_Use_api.

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)

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

After that just save your new Customer_In_Use

an example of create function :

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)

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