2014-09-19 14 views
5

Tôi mới tham gia DRF và tôi gặp sự cố khi ghi đè phương pháp của nó.Django Rest Framework - Viewsets và phương pháp ghi đè để cho phép nhiều hình thức giá trị tra cứu

Tôi muốn cho phép một Chế độ xem chấp nhận hai dạng khác nhau của cùng một giá trị tra cứu, về bản chất là chuỗi con của giá trị và chuỗi đầy đủ.

Ví dụ: vị trí và pk db, vị trí .namespace.host.com

tôi đã có thể ghi đè lên thành công phương pháp get_object để cho phép cả hai giá trị cho một yêu cầu GET, nhưng nếu tôi cố gắng để gửi một POST để cập nhật trạng thái, sau đó nó không hoạt động. Vì nó là một pk, nó cố gắng tạo một mục nhập db mới với pk = vị trí và không thành công.

Có vẻ như không vui khi tôi thay đổi kwarg (nó sẽ quay trở lại vị trí cũ khi chạy chức năng trang trí csrf_exempt từ thư viện DRF).

Cách thích hợp để thực hiện việc này là gì?

from rest_framework import serializers 
from myapp.apps.clusters.models import Status 
from myapp.com.mixins.ViewSets import CreateUpdateListRetrieveViewSet 
from django.http import Http404 
from rest_framework.generics import GenericAPIView 
from rest_framework.mixins import UpdateModelMixin 


class StatusSerializer(serializers.ModelSerializer): 
    class Meta: 
     model = Status 
     lookup_field = 'hostname' 


class StatusViewSet(CreateUpdateListRetrieveViewSet): 
    model = Status 
    serializer_class = StatusSerializer 
    lookup_field = 'hostname' 

    def get_object(self, queryset=None): 
     """ 
     Overrides the GenericView method to allow for both hostname and physname lookups 

     i.e. physname (location) or full FQDN (location.namespace.host.com) 

     Returns the object the view is displaying. 
     """ 
     try: 
      obj = GenericAPIView.get_object(self) 
     except Http404: 
      hostname = self.kwargs.get(self.lookup_field, None) + '.namespace.host.com' 
      self.kwargs[self.lookup_field] = hostname 
      obj = GenericAPIView.get_object(self)  
     return obj 

    def update(self, request, *args, **kwargs): 
     """ This doesn't play nice, overriding UpdateModelMixin's update """ 
     lookup_field = self.kwargs.get(self.lookup_field, None) 
     if not '.namespace.host.com' in lookup_field 
      hostname = lookup_field + '.namespace.host.com' 
      self.kwargs['hostname'] = hostname 
      kwargs['hostname'] = hostname 
     return UpdateModelMixin.update(self, request, *args, **kwargs) 

    def pre_save(self, obj): 
     """ This was an attempt but the code is not currently hit """ 
     if not '.namespace.host.com' in self.kwargs['hostname']: 
      self.kwargs['hostname'] = self.kwargs['hostname'] + '.namespace.host.com' 

Cảm ơn bạn đã được trợ giúp!

Trả lời

0

Tại sao không ghi đè phương thức __init__ của chế độ xem?

class StatusViewSet(CreateUpdateListRetrieveViewSet): 
    model = Status 
    serializer_class = StatusSerializer 
    lookup_field = 'hostname' 

    def __init__(self, *args, **kwargs): 
     if 'hostname' in kwargs and '.namespace.host.com' not in kwargs['hostname']: 
      kwargs['hostname'] = kwargs['hostname'] + '.namespace.host.com' 
     super(StatusViewSet, self).__init(*args, **kwargs) 
Các vấn đề liên quan