我尝试根据登录的用户过滤POST的序列化程序字段。
serializers.py
class WidgetListSerializer(EncryptedLookupModelSerializer):
owner = serializers.HiddenField(default=serializers.CurrentUserDefault())
parent = serializers.SlugRelatedField(
slug_field='name',
queryset=WidgetList.objects.filter(owner=serializers.CurrentUserDefault()))
class Meta:
model=WidgetList
fields = ('id', 'name', 'description', 'owner', 'parent')
回溯
Traceback:
File "/opt/myproject_app/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
119. resolver_match = resolver.resolve(request.path_info)
File "/opt/myproject_app/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
365. for pattern in self.url_patterns:
File "/opt/myproject_app/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
401. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/opt/myproject_app/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
395. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/opt/myproject/myproject/urls.py" in <module>
20. from gui import views
File "/opt/myproject/gui/views.py" in <module>
20. from api.serializers import *
File "/opt/myproject/api/serializers.py" in <module>
52. class WidgetListSerializer(EncryptedLookupModelSerializer):
File "/opt/myproject/api/serializers.py" in WidgetListSerializer
57. queryset=WidgetList.objects.filter(owner=serializers.CurrentUserDefault()))
File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
127. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/query.py" in filter
679. return self._filter_or_exclude(False, *args, **kwargs)
File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
697. clone.query.add_q(Q(*args, **kwargs))
File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
1310. clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q
1338. allow_joins=allow_joins, split_subq=split_subq,
File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_filter
1156. value, lookups, used_joins = self.prepare_lookup_value(value, lookups, can_reuse, allow_joins)
File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/sql/query.py" in prepare_lookup_value
1003. value = value()
File "/opt/myproject_app/lib/python2.7/site-packages/rest_framework/fields.py" in __call__
242. return self.user
Exception Type: AttributeError at /api/widgetlists/
Exception Value: 'CurrentUserDefault' object has no attribute 'user'
答案 0 :(得分:1)
为了能够使用登录用户,您需要先访问请求对象(request.user
)
以下是如何自定义序列化程序的示例:
class WidgetListSerializer(EncryptedLookupModelSerializer):
# ...
def __init__(self, *args, **kwargs):
user = kwargs['context']['request'].user
super(WidgetListSerializer, self).__init__(*args, **kwargs)
self.fields['owner'].default = user
self.fields['parent'].queryset = WidgetList.objects.filter(owner=user)