如何使用django rest framwork的perform_create传递额外的参数?

时间:2016-09-16 01:47:46

标签: python django python-3.x django-rest-framework django-serializer

我正在使用django rest frameworks通用视图。我试图在post的作者字段中插入请求用户名。

串行器

class PostSerializer(serializers.ModelSerializer):
    spoter = serializers.PrimaryKeyRelatedField(
        queryset= User.objects.all(),
        )

class Meta:
    model = PostModel
    fields = ('author','text')

查看

class UserRequestMixin(object):
    def perform_update(self, serializer):
        serializer.save(author = self.request.user.id)

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

class PostViewSet(UserRequestMixin,DefaultsMixin,viewsets.ModelViewSet):

    permission_classes = (IsOwnerOrReadOnly,)
    queryset = PostModel.objects.all()
    serializer_class = PostSerializer

错误

 status -> 400
 {
    "author": [
      "This field is required."
     ] 
  } 

3 个答案:

答案 0 :(得分:0)

根据您的要求,您应该:

  • 将作者字段标记为required=Falseclass PostSerializer(serializers.ModelSerializer): spoter = serializers.PrimaryKeyRelatedField( queryset= User.objects.all(), ) class Meta: model = PostModel fields = ('author','text') extra_kwargs = { 'author': { 'read_only': True, 'default': serializers.CurrentUserDefault(), } }
  • 使用CreateOnlyDefault
  • 设置作者字段

例如:

<?xml version="1.0" encoding="UTF-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="Phoneword.MainPage">
         <ContentPage.Padding>
            <OnPlatform x:TypeArguments="Thickness"
                        iOS="20, 40, 20, 20"
                        Android="20, 20, 20, 20"
                        WinPhone="20, 20, 20, 20" />
         </ContentPage.Padding>
        <ContentPage.Content>
            <StackLayout VerticalOptions="FillAndExpand"
                         HorizontalOptions="FillAndExpand"
                         Orientation="Vertical"
                         Spacing="15">
                <Label Text="Enter a Phoneword:" />
                <Entry x:Name="phoneNumberText" Text="1-855-XAMARIN" />
                <Button x:Name="translateButon" Text="Translate" Clicked="OnTranslate" />
                <Button x:Name="callButton" Text="Call" IsEnabled="false" Clicked="OnCall" />
                <Button x:Name="callHistoryButton" Text="Call History" IsEnabled="false"
              Clicked="OnCallHistory" />
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>

答案 1 :(得分:0)

你应该在你的PostViewSet中添加一个perform_create(),指定在保存时必须填写的字段的名称:

class PostViewSet(viewsets.ModelViewSet):

    permission_classes = (IsOwnerOrReadOnly,)
    queryset = PostModel.objects.all()
    serializer_class = PostSerializer

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

请记住你的Post模型应该有一个作者字段

答案 2 :(得分:0)

只需从序列化程序字段中排除 serializer.save 字段即可。调用 request.data 时,您可以传递任何字段,不一定是序列化程序的字段。

序列化器字段是包含在 useEffect 中的字段,而作者不是这样的字段。