serializers.py
from rest_framework import serializers
from .models import Flight, Segment, Airport
class DynamicFieldsModelSerializer(serializers.ModelSerializer):
"""
A ModelSerializer that takes an additional `fields` argument that
controls which fields should be displayed.
"""
def __init__(self, *args, **kwargs):
# Instantiate the superclass normally
super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)
fields = self.context['request'].query_params.get('fields')
if fields:
fields = fields.split(',')
# Drop any fields that are not specified in the `fields` argument.
allowed = set(fields)
existing = set(self.fields.keys())
for field_name in existing - allowed:
self.fields.pop(field_name)
class SegmentSerializer(serializers.ModelSerializer):
class Meta:
model = Segment
fields = ( # TODO - Could be __all__ if no fine tuning
'id', 'flight_id', 'dep_code', ...
)
class FlightSerializer(DynamicFieldsModelSerializer, serializers.ModelSerializer):
segments = SegmentSerializer(many=True, source='segment_set')
class Meta:
model = Flight
fields = ( # TODO - Could be __all__ if no fine tuning
'id', 'dep_air', 'dest_air', ...
)
class AirportSerializer(DynamicFieldsModelSerializer, serializers.ModelSerializer):
dep_air = FlightSerializer(many=False, source='dep_air_airport')
class Meta:
model = Airport
fields = ('iata_code', 'name', 'continent', 'iso_country',)
启动服务器时出现以下错误:
File "/Users/me/PycharmProjects/fly_baby/flight_data/serializers.py", line 55, in AirportSerializer
dep_air = FlightSerializer(many=False, source='dep_air_airport')
File "/Users/me/PycharmProjects/fly_baby/flight_data/serializers.py", line 15, in __init__
fields = self.context['request'].query_params.get('fields')
KeyError: 'request'
目标是使航班嵌套在机场下,反之亦然,但是当我使用DynamicFieldsModelSerializer mixin时似乎不可能。 init 询问以下行不存在的self.context ['request']
dep_air = FlightSerializer(many=False, source='dep_air_airport')
我相信我本来是要传递上下文的,但是我不知道对于我的泛型设置来说这是怎么可能的。
其他代码::
views.py
class AirportFlightViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = FlightSerializer
def get_queryset(self):
return Flight.objects.filter(flight=self.kwargs['airport_pk'])
urls.py
router = DefaultRouter()
router.register(r'flights', views.FlightViewSet)
router.register(r'segments', views.SegmentViewSet)
router.register(r'airports', views.AirportViewSet)
flights_router = routers.NestedSimpleRouter(router, r'flights', lookup='flight')
flights_router.register(r'segments', views.FlightSegmentViewSet, basename='flight-segments')
airports_router = routers.NestedSimpleRouter(router, r'airports', lookup='airport')
airports_router.register(r'flights', views.AirportFlightViewSet, basename='airport-flights')
urlpatterns = [
path('', views.index),
path('api/', include(router.urls)),
path('api/', include(flights_router.urls)),
path('api/', include(airports_router.urls)),
]
答案 0 :(得分:1)
您可以编辑DynamicFieldsModelSerializer
以不依赖于存在的上下文,因此可以通过使用.get()
来构造它而无需它:
class DynamicFieldsModelSerializer(serializers.ModelSerializer):
"""
A ModelSerializer that takes an additional `fields` argument that
controls which fields should be displayed.
"""
def __init__(self, *args, **kwargs):
# Instantiate the superclass normally
super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)
request = self.context.get('request')
if request:
fields = request.query_params.get('fields')
if fields:
... # code as before
但是请注意,当像这样使用子序列化程序(在这种情况下为FlightSerializer
)时,将始终应用所有字段。但是,这很有道理,因为在请求机场时,您希望fields
参数会影响机场而不是航班的字段。