Django Rest Framework“ myusers”“此字段为必填

时间:2019-10-05 14:40:10

标签: python django django-rest-framework

我正在学习DRF,现在遇到了使我停滞了好几天的问题。该应用程序是zumba类应用程序,我正在使用DRF在其之上创建API。我现在尝试构建的部分是用户可以将自己添加到zumba类的部分(因此,他必须能够更新manytomany字段)当用户注册自己时,我希望API做什么到该类(PUT或PATCH)的方法是获取我们从身份验证中获得的用户名,并将其添加到“ myusers”字段中。但是由于PUT为空,因此API一直抱怨“ myusers”是必需的。

是否有一种方法告诉API PUT请求中不需要“ myusers”,因为它是从身份验证令牌中提取的? (如果我在正文中使用"myusers": [{"id": 9}]手动创建一个PUT请求,它可以工作,但是我想避免添加该客户端,因为甚至没有使用传递的数据。)

序列化程序(所有只读字段都是为了确保用户无法更新它们):

class UserActivityClassesSerializer(serializers.ModelSerializer):
    activitytypename = serializers.SlugRelatedField(source='activitytype', slug_field='name', read_only=True)
    activitytype = ActivityTypeSerializer(read_only=True)
    myusers = MyUsersSerializer(many=True) # serializers.HyperlinkedRelatedField(many=True, view_name='myusers-detail', read_only=True) 
    uri = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Activity
        fields = [
            'uri',
            'id',
            'name',
            'date',
            'activitytype',
            'activitytypename',
            'status',
            'myusers',
        ]
        read_only_fields = [
            'uri',
            'id',
            'name',
            'date',
            'activitytype',
            'activitytypename',
            'status',
        ]


    def get_uri(self, obj):
        request = self.context.get('request')
        return api_reverse("api-activities:classes-detail", kwargs={"id": obj.id}, request=request)

视图

class ActivityViewSet(viewsets.ModelViewSet):
    permission_classes = [permissions.IsAuthenticated]
    queryset = Activity.objects.all()
    lookup_field = 'id'

    def get_serializer_class(self):
        if self.request.user.is_staff or self.action == 'create':
            return AdminActivityClassesSerializer
        else:
            return UserActivityClassesSerializer

    def perform_update(self, serializer):
        instance = self.get_object()
        request = serializer.context['request']
        auth_user = request.user
        qs = instance.myusers.filter(id=auth_user.id)#we verify is the user is already registered, and if yes, we remove him
        if qs:
            print('delete')
            instance.myusers.remove(auth_user)
            return instance
        else:
            result = verify_valid_season_pass(auth_user, instance)
            if result == 1:
                print('add')
                instance.myusers.add(auth_user.id)
            else:
                raise serializers.ValidationError("No valid seasonpass found.")

要更新的模型:

class Activity(models.Model):
    CLOSE = 0
    OPEN = 1
    ACTIVITY_STATUS_CHOICES = [
        (CLOSE, 'Closed'),
        (OPEN, 'Open'),
    ]
    name            = models.CharField('Activity Name', max_length=64, blank=False, null=False)
    date            = models.DateTimeField('Start Date & Time', blank=False, null=False)
    activitytype    = models.ForeignKey(ActivityType, blank=False, null=False, default=1, on_delete=models.DO_NOTHING)
    status          = models.IntegerField('Status', choices=ACTIVITY_STATUS_CHOICES, default=OPEN,)  # 1 = open, 0 = closed
    myusers         = models.ManyToManyField("users.User")

    def __str__(self):
        return self.name

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

您尝试过

myusers = MyUsersSerializer(many=True, required=False)