django休息框架错误需要一个字段

时间:2018-03-01 08:48:46

标签: python django django-rest-framework

我一直在http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/关注教程(这很不错),但我已经到了最后,我正在运行命令

http -a admin:password123 POST http://127.0.0.1:8000/snippets/ code =" print 789"

它给了我一个错误:

  

HTTP / 1.1 400错误请求允许:GET,POST,HEAD,OPTIONS   内容长度:37内容类型:application / json日期:2月28日星期三   2018 18:29:15 GMT服务器:WSGIServer / 0.2 CPython / 3.6.3变化:接受,   Cookie X-Frame-Options:SAMEORIGIN

     

{       "所有者":[           "此字段是必填字段。"       ]}

对于我创建的所有用户,浏览器api提供选项的所有者字段也是可见的。保存它(浏览器或命令行)时,它会保存发出请求的用户,以便部件正确。我认为它不应该在可浏览的api上可见,并且在api调用中不需要它,因为它从请求中找出它。

这是我的代码:

views.py:

class SnippetList(generics.ListCreateAPIView):
  queryset = Snippet.objects.all()
  serializer_class = SnippetSerializer
  permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

  def perform_create(self, serializer):
    serializer.save(owner=self.request.user)

models.py:

class Snippet(models.Model):
  created = models.DateTimeField(auto_now_add=True)
  title = models.CharField(max_length=100, blank=True, default='')
  code = models.TextField()
  linenos = models.BooleanField(default=False)
  language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
  style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)
  owner = models.ForeignKey('auth.User', related_name='snippets', on_delete=models.CASCADE)
  highlighted = models.TextField()

  class Meta:
    ordering = ('created',)

  def save(self, *args, **kwargs):
    lexer = get_lexer_by_name(self.language)
    linenos = self.linenos and 'table' or False
    options = self.title and {'title': self.title} or {}
    formatter = HtmlFormatter(style=self.style, linenos=linenos, full=True, **options)
    self.highlighted = highlight(self.code, lexer, formatter)
    super(Snippet, self).save(*args, **kwargs)

serializers.py

class SnippetSerializer(serializers.ModelSerializer):
  class Meta:
    model = Snippet
    fields = ('id', 'title', 'code', 'linenos', 'language', 'style', 'owner')
    owner = serializers.ReadOnlyField(source='owner.username')

2 个答案:

答案 0 :(得分:2)

在序列化程序中,您将字段定义为类的属性,而不是在Meta中。试试这个:

class SnippetSerializer(serializers.ModelSerializer):
  owner = serializers.ReadOnlyField(source='owner.username')
  class Meta:
    model = Snippet
    fields = ('id', 'title', 'code', 'linenos', 'language', 'style', 'owner')

答案 1 :(得分:0)

class SnippetList(generics.ListCreateAPIView):
    queryset = Snippet.objects.all()
    serializer_class = SnippetSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

    def perform_create(self, serializer):
        serializer.validated_data['owner'] = self.request.user
        serializer.save()

class SnippetSerializer(serializers.ModelSerializer):
      class Meta:
      model = Snippet
      fields = ('id', 'title', 'code', 'linenos', 'language', 'style', 'owner')
      read_only_fields = ('owner',)

您必须在序列化程序验证数据中指定值。