如何在Django的序列化器中获取当前用户?

我试图让作者在我创建文章的地方。我尝试了各种方法,但遇到很多错误。 例如我试过

author= self.context['request'].user.profile

但我收到数据库错误。 我尝试了1或2个以上可以从StackOverFlow进行的尝试,但是没有机会找到我的机会。有人可以帮忙吗?

Views.py

class ArticleCreateView(CreateAPIView):

    serializer_class = ArticleCreateSerializer
    permission_classes = (IsAuthenticated,)

    def create(self, request):
        serializer_context = {
            'request': request
        }
        serializer_data = request.data.get('article',{})
        serializer = self.serializer_class(
        data=serializer_data, context=serializer_context,
        )
        serializer.is_valid(raise_exception=True)
        serializer.save()
        response = {
            'success' : 'True',
            'status code' : status.HTTP_200_OK,
            'message': 'Article Created Successfully!',
            }
        status_code = status.HTTP_200_OK
        return Response(serializer.data, status=status.HTTP_201_CREATED)

Serializers.py

class ArticleCreateSerializer(serializers.ModelSerializer):
    caption = serializers.CharField(required=False)
    author = UserSerializer(read_only=True)

    class Meta:
        model = Articles
        fields = ('id','author','caption')

    def create(self, validated_data):
        article = Articles.objects.create(author=author,**validated_data)
        return article

错误

  article = Article.objects.create(**validated_data)
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 422, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line 740, in save
    self.save_base(using=using, force_insert=force_insert,
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line 777, in save_base
    updated = self._save_table(
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line 870, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line 907, in _do_insert
    return manager._insert([self], fields=fields, return_id=update_pk,
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 1186, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1375, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 99, in execute
    return super().execute(sql, params)
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Umar\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\cursor.py", line 59, in execute
    raise db_exe from e
django.db.utils.DatabaseError
[17/May/2020 16:49:56] "POST /api/createarticle/ HTTP/1.1" 500 271975
评论