Django Rest Framework错误:{“user”:[“此字段是必填项。”]

时间:2017-10-20 03:48:15

标签: django django-rest-framework

发布时:

curl -X POST  -H "Authorization: Token sometoken" -d "url=someurl" 127.0.0.1:8000/create/

我收到错误:

  

{“user”:[“此字段是必填项。”],带有ItemSerializer,

我在SO上看过其他关于使用perform_create的帖子,我试图用它来保存用户对象,但由于某些原因它不起作用。当用户定义如下时,Perform_create有效:

user = serializers.CharField(
    default=serializers.CurrentUserDefault()
)

但我想使用用户对象,而不仅仅是存储用户名的CharField

串行器:

class UserDetailsSerializer(serializers.ModelSerializer):
  class Meta:
    model = UserModel
    fields = ('pk', 'username', 'email', 'first_name', 'last_name')
    read_only_fields = ('email', )


class CategorySerializer(serializers.ModelSerializer):
  class Meta:
    model = Category
    fields = ['cat']

 class CommentSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
         model = Comment
         fields = [
             'comment',

         ]

class ItemSerializer(serializers.HyperlinkedModelSerializer):
     user = UserDetailsSerializer()
     category = CategorySerializer(many=True)
     thecomments = CommentSerializer(many=True)
     timestamp = serializers.SerializerMethodField('get_mytimestamp')

     def get_mytimestamp(self, obj):
         return time.mktime(datetime.datetime.now().timetuple())

     class Meta:
         model = Item

         fields = [
         "url",
         "user",
         "timestamp",
         "categories",
         "thecomments",
    ]

型号:

class Item(models.Model):
    url = models.CharField(max_length=1000)
    user = models.ForeignKey('auth.User', unique=False)
    timestamp = models.DateTimeField(auto_now_add=True)
    url = models.CharField(max_length=1000)
    categories = models.ManyToManyField(Category)

查看:

class ItemCreateAPIView(generics.CreateAPIView):
     serializer_class = ItemSerializer

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

解决方案:

用于创建项目的序列化程序:

class CreateSerializer(serializers.HyperlinkedModelSerializer):
     timestamp = serializers.SerializerMethodField('get_mytimestamp')

     def get_mytimestamp(self, obj):
         return time.mktime(datetime.datetime.now().timetuple())

     class Meta:
         model = Item

         fields = [
         "url",
         "timestamp",
         ]

views.py

class ItemCreateAPIView(generics.CreateAPIView):
     serializer_class = CreateSerializer

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

帖子:

curl -X POST  -H "Authorization: Token sometoken" -d "url='someurl'" 127.0.0.1:8000/createitem/

1 个答案:

答案 0 :(得分:2)

class ItemSerializer(serializers.HyperlinkedModelSerializer):
     user = UserDetailsSerializer()
     category = CategorySerializer(many=True)
     thecomments = CommentSerializer(many=True)
     timestamp = serializers.SerializerMethodField('get_mytimestamp')

     def get_mytimestamp(self, obj):
         return time.mktime(datetime.datetime.now().timetuple())

     class Meta:
         model = Item

         fields = [
         "url",
         "user",
         "timestamp",
         "categories",
         "thecomments",
         ]
        extra_kwargs = {'user': {'required': False}}

extra_kwargs = {'user': {'required': False}}添加到序列化工具Meta