如何从序列化器获取数据参数?

时间:2017-04-26 13:04:20

标签: python django django-rest-framework serializer

我正在使用Django和django-restframework。

我不知道如何访问Serializer的论点。

我可以从serializer.data访问完整的json数据。但是当我试图获得像id这样的参数时,它会返回错误。 serializer.data.id

我怎样才能得到它?我花了很多时间搜索这个,但失败了。

我这样做是因为根据用户输入更新模型。

e.g。

  1. 用户输入id
  2. 获取id
  3. 的数据
  4. 将该数据保存到数据库。
  5. 我非常感谢您阅读本文。

    views.py

    class CareerViewSet(viewsets.ModelViewSet):
        queryset = Career.objects.all()
        serializer_class = CareerSerializer
        permission_classes = (IsCreateable,)
    
        def perform_create(self, serializer):
            serializer.save(nickname='test')
            print(serializer.data)
            print(serializer.data.id)
    

    错误

    {'id': 14, 'created': '2017-04-26T12:39:58.249038Z', 'modified': '2017-04-26T12:39:58.249538Z', 'battle_tag': 'Fortune-1130', 'nickname': 'test', 'quick_eliminations': None, 'quick_damage_done': None, 'quick_deaths': None, 'quick_final_blows': None, 'quick_healing_done': None, 'quick_objective_kills': None, 'quick_objective_time': None, 'quick_solo_kills': None, 'competitive_eliminations': None, 'competitive_damage_done': None, 'competitive_deaths': None, 'competitive_final_blows': None, 'competitive_healing_done': None, 'competitive_objective_kills': None, 'competitive_objective_time': None, 'competitive_solo_kills': None, 'games_won': None, 'competitive_rank': None, 'level': None}
    Internal Server Error: /career/
    Traceback (most recent call last):
      File "E:\Development\Venv\20170420\myvenv\lib\site-packages\django\core\handlers\exception.py", line 42, in inner
        response = get_response(request)
      File "E:\Development\Venv\20170420\myvenv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "E:\Development\Venv\20170420\myvenv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "E:\Development\Venv\20170420\myvenv\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view
        return view_func(*args, **kwargs)
      File "E:\Development\Venv\20170420\myvenv\lib\site-packages\rest_framework\viewsets.py", line 83, in view
        return self.dispatch(request, *args, **kwargs)
      File "E:\Development\Venv\20170420\myvenv\lib\site-packages\rest_framework\views.py", line 483, in dispatch
        response = self.handle_exception(exc)
      File "E:\Development\Venv\20170420\myvenv\lib\site-packages\rest_framework\views.py", line 443, in handle_exception
        self.raise_uncaught_exception(exc)
      File "E:\Development\Venv\20170420\myvenv\lib\site-packages\rest_framework\views.py", line 480, in dispatch
        response = handler(request, *args, **kwargs)
      File "E:\Development\Venv\20170420\myvenv\lib\site-packages\rest_framework\mixins.py", line 21, in create
        self.perform_create(serializer)
      File "E:\Development\Venv\20170420\overmatch_django\overwatch\views.py", line 38, in perform_create
        print(serializer.data.id)
    AttributeError: 'ReturnDict' object has no attribute 'id'
    

2 个答案:

答案 0 :(得分:3)

serializer.data是一个字典子类(即ReturnDict),像访问一个字母一样访问它:

id  = serializer.data['id']

答案 1 :(得分:1)

使用serializer.data.get(' id')。 serializer.data.id doest不工作​​,因为它不是一个类对象,而是一个json / dict。