WidgetListACL匹配查询不存在

时间:2017-03-19 15:35:03

标签: python django django-rest-framework

我试图让序列化程序在字段中显示已过滤的列表,如下所示:

class WidgetListSerializer(TaggitSerializer, ModelSerializer):

    parent = SlugRelatedField(
        slug_field='name',
        queryset=WidgetList.objects.all(),
        allow_null=True)
    tags = TagListSerializerField()

    def create(self, validated_data):
        user = ProjectUser.objects.get(username=self.context['request'].user)
        widgetlist = super(WidgetListSerializer, self).create(validated_data)
        level = AccessLevel.objects.get(pk=1)
        WidgetListACL.objects.create(user=user, list=widgetlist, level=level)
        return passwordlist

    class Meta:
        model=WidgetList
        fields = ('id', 'name', 'description', 'parent', 'tags', 'created', 'modified')

    def __init__(self, *args, **kwargs):
        user = kwargs['context']['request'].user

        super(WidgetListSerializer, self).__init__(*args, **kwargs)
        self.fields['parent'].queryset = WidgetListACL.objects.select_related('list').get(user_id=user)

不幸的是我收到了以下追溯:

File "D:\Python35\lib\site-packages\django\core\handlers\exception.py" in inner
  39.             response = get_response(request)

File "D:\Python35\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "D:\Python35\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "D:\Python35\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "D:\Python35\lib\site-packages\rest_framework\viewsets.py" in view
  83.             return self.dispatch(request, *args, **kwargs)

File "D:\Python35\lib\site-packages\rest_framework\views.py" in dispatch
  477.             response = self.handle_exception(exc)

File "D:\Python35\lib\site-packages\rest_framework\views.py" in handle_exceptiona
  437.             self.raise_uncaught_exception(exc)

File "D:\Python35\lib\site-packages\rest_framework\views.py" in dispatch
  474.             response = handler(request, *args, **kwargs)

File "D:\Projects\enterpass\api\widgetlists\views.py" in list
  24.         serializer = WidgetListSerializer(queryset, many=True, context={'request': request})

File "D:\Python35\lib\site-packages\rest_framework\serializers.py" in __new__
  124.             return cls.many_init(*args, **kwargs)

File "D:\Python35\lib\site-packages\rest_framework\serializers.py" in many_init
  145.         child_serializer = cls(*args, **kwargs)

File "D:\Projects\enterpass\api\widgetlists\serializers.py" in __init__
  29.         self.fields['parent'].queryset = WidgetListACL.objects.select_related('list').get(user_id=user)

File "D:\Python35\lib\site-packages\django\db\models\query.py" in get
  385.                 self.model._meta.object_name

Exception Type: DoesNotExist at /api/v1/widgetlists/
Exception Value: WidgetListACL matching query does not exist.

如果我删除了self.fields的行,那么它可以正常工作,但不会过滤每个用户的列表。

0 个答案:

没有答案