'RelatedManager'对象不可迭代

时间:2014-09-26 17:36:32

标签: django django-rest-framework

在Django Rest Framework中,我有以下json,我正在尝试PUT

{
    "trgmst_id": 2244, 
    "trgmst_type": 1, 
    "trgmst_name": "evnt_job                      ", 
    "trgmst_desc": null, 
    "trgmst_public": "Y", 
    "trgmst_active": "Y", 
    "trgmst_trigger": 10, 
    "owner_name": "Operations                    ", 
    "trgmst_jobctl": 1, 
    "trgmst_alljobs": "N", 
    "trgmst_lstchgtm": "2014-09-17T16:05:32.463", 
    "trgmst_parm": "OP=0,EX1=1,EX2=1", 
    "trgmst_maxreruns": 0, 
    "nodmst_name": null, 
    "servicemst_name": null, 
    "trgmst_info": null, 
    "trgmst_monitor": 1, 
    "trgmst_trgtskcnt": 0, 
    "trgmst_lasttrgtskrun": null, 
    "event_action_details": [
        {
            "trgtsk_order": 1, 
            "trgtsk_id": 2977, 
            "trgmst_id": 2244, 
            "tskmst_id": null, 
            "trgtsk_type": 1
        }
    ]
}

在提交PUT请求时,我得到以下回溯 -

Traceback:
File "D:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Python27\lib\site-packages\django\views\generic\base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "D:\Python27\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "D:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
  400.             response = self.handle_exception(exc)
File "D:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
  397.             response = handler(request, *args, **kwargs)
File "D:\Tidal\API\views.py" in put
  204.         if serializer.is_valid():
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in is_valid
  553.         return not self.errors
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in errors
  545.                 ret = self.from_native(data, files)
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in from_native
  996.         instance = super(ModelSerializer, self).from_native(data, files)
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in from_native
  368.             attrs = self.restore_fields(data, files)
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in restore_fields
  283.                 field.field_from_native(data, files, field_name, reverted_data)
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in field_from_native
  472.                 if serializer.is_valid():
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in is_valid
  553.         return not self.errors
File "D:\Python27\lib\site-packages\rest_framework\serializers.py" in errors
  521.                     identities = [self.get_identity(self.to_native(obj)) for obj in objects]

Exception Type: TypeError at /deploy/event/
Exception Value: 'RelatedManager' object is not iterable

这是一个相当简单的视图/序列化器。这是我的序列化器。请注意,它包含反向关系 -

class EventActionSerializer(serializers.ModelSerializer):

    class Meta:
        model = Trgtsk
        resource_name = 'trgtsk'
        fields = ('trgtsk_order', 'trgtsk_id', 'trgmst_id', 'tskmst_id', 'trgtsk_type')

class EventSerializer(serializers.ModelSerializer):
    owner_name = serializers.Field(source='owner_id.owner_name')
    nodmst_name = serializers.Field(source='nodmst_id.nodmst_name')
    servicemst_name = serializers.Field(source='servicemst_id.servicemst_name')
    event_action_details = EventActionSerializer()

    class Meta:
        model = Trgmst
        resource_name = 'trgmst'
        depth = 1
        fields = ('trgmst_id', 'trgmst_type', 'trgmst_name', 'trgmst_desc', 'trgmst_public', 'trgmst_active',
                'trgmst_trigger', 'owner_name', 'trgmst_jobctl', 'trgmst_alljobs', 'trgmst_lstchgtm', 'trgmst_parm',
                'trgmst_maxreruns', 'nodmst_name', 'servicemst_name', 'trgmst_info', 'trgmst_monitor', 'trgmst_trgtskcnt',
                'trgmst_lasttrgtskrun', 'event_action_details')

视图只是标准的API视图。当没有任何相关模型与我的任何其他观点时,我没有问题。

class DeployEventView(APIView):

    def get_object(self, pk):
        try:
            return Trgmst.objects.get(pk=pk)
        except Trgmst.DoesNotExist:
            raise Http404

    def get(self, request, format=None):
        name = self.request.GET.get('name')
        pk = Trgmst.objects.filter(trgmst_name=name).values_list('trgmst_id', flat=True)
        trgmst = self.get_object(pk)
        serializer = EventSerializer(trgmst)
        return Response(serializer.data)

    def put(self, request, format=None):
        name = self.request.GET.get('name')
        pk = Trgmst.objects.filter(trgmst_name=name).values_list('trgmst_id', flat=True)
        trgmst = self.get_object(pk)
        serializer = EventSerializer(trgmst, data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, format=None):
        name = self.request.GET.get('name')
        pk = Trgmst.objects.filter(trgmst_name=name).values_list('trgmst_id', flat=True)
        trgmst = self.get_object(pk)
        trgmst.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

1 个答案:

答案 0 :(得分:0)

奇怪的是,在我的序列化程序中,我在EventSerializer中更改了以下行 -

class EventActionSerializer(serializers.ModelSerializer):

class Meta:
    model = Trgtsk
    resource_name = 'trgtsk'
    fields = ('trgtsk_order', 'trgtsk_id', 'trgmst_id', 'tskmst_id', 'trgtsk_type')

class EventSerializer(serializers.ModelSerializer):
    owner_name = serializers.Field(source='owner_id.owner_name')
    nodmst_name = serializers.Field(source='nodmst_id.nodmst_name')
    servicemst_name = serializers.Field(source='servicemst_id.servicemst_name')
    event_action_details = EventActionSerializer

    class Meta:
        model = Trgmst
        resource_name = 'trgmst'
        depth = 1
        fields = ('trgmst_id', 'trgmst_type', 'trgmst_name', 'trgmst_desc', 'trgmst_public', 'trgmst_active',
                'trgmst_trigger', 'owner_name', 'trgmst_jobctl', 'trgmst_alljobs', 'trgmst_lstchgtm', 'trgmst_parm',
                'trgmst_maxreruns', 'nodmst_name', 'servicemst_name', 'trgmst_info', 'trgmst_monitor', 'trgmst_trgtskcnt',
                'trgmst_lasttrgtskrun', 'event_action_details')

特别是我从行event_action_details = EventActionSerializer中删除了(),现在如果对象存在于两个模型中,我可以很好地。如果它是新的,它会抱怨event_action_details.trgmst_id不存在,如果它试图同时进行这两个操作就不会存在吗?