如何在Django Rest框架中添加自定义方法

时间:2019-07-13 10:51:15

标签: python django

得益于Django的其余框架,我可以轻松处理模型的原始API,但是如果我想将自定义api添加到预订模型中,例如下面的代码,该怎么办: 这是我的序列化器:

    from rest_framework import serializers
from .models import Booking

class BookingSerializer(serializers.ModelSerializer):
     class Meta:
         model = Booking
         fields =(
             'user_id',
             'computed_net_price',
             'final_net_price',
             'payable_price',
             'booking_status',
             'booking_status',
             'guest_name',
             'guest_last_name',
             'guest_cellphone',
             'guest_cellphone',
             'guest_cellphone',
             'operator_id',
             'is_removed',
         )

和我的观点:

   from rest_framework import viewsets
from .models import Booking
from booking.serializers import BookingSerializer


# Create your views here.
class BookingView(viewsets.ModelViewSet):

                queryset = Booking.objects.all()
                serializer_class = BookingSerializer

和我的网址:

   from django.urls import path,include
from . import views
from rest_framework import routers

router = routers.DefaultRouter()
router.register('Booking',views.BookingView)
urlpatterns = [
    #path('',include('facility.urls'))
    path('',include(router.urls))
]

例如,如果我想添加新的api名称,例如bookingapi,我是否应该在同一文件中复制所有内容???或使新文件在模型中具有相同的名称,或者我该怎么做

0 个答案:

没有答案