在django rest框架中将对象添加到多个到多个字段时阻止创建相关对象

时间:2016-10-01 16:52:56

标签: python django serialization django-rest-framework many-to-many

我已经提到过这样的many posts和一个关于DRF的github回购的bug。我仍然不清楚这是否仍然存在错误,因为我到目前为止获取的信息不是来自最近的帖子。我正在尝试使用django rest框架创建一个api。下面给出的是我的代码。

Serializers.py

boolean isLetter(char let){
        return ( let>= 'a'&& let <= 'z') || ( let >= 'A' && let <='Z');
}

public static void main(String args[]){
    File file_in_obj = new File("E:/Problems","notes.txt");
    File file_out_obj = new File("E:/Problems","notes_sorted.txt");
...
}

Views.py

class MemberSerializer(serializers.ModelSerializer):

class Meta:
    model = User
    fields = ('username', 'email')

class TeamSerializer(serializers.ModelSerializer):
    members = MemberSerializer(many=True)

    class Meta:
        model = Team
        fields = ('name', 'members')
class TeamUpdateSerializer(serializers.ModelSerializer):

    class Meta:
        model = Team
        fields = ('name', 'description', 'members', 'team_type', 'created_by')

成员与django用户模型的class TeamViewSet(viewsets.ModelViewSet): queryset = Team.objects.all() serializer_class = TeamSerializer authentication_classes = (SessionAuthentication, BasicAuthentication) permission_classes = (IsAuthenticated,) def get_queryset(self): queryset = Team.objects.filter(created_by=self.request.user) return queryset 关系。我可以创建一个新对象,获取详细信息,但在更新现有的Team模型时,通过向成员字段添加新用户,它会显示M2M。我可以在a user with that username exists的{​​{1}}方法中覆盖save()方法或switch to another serializer来解决问题。

update()

但我不确定这是否正确。如果设计了一种解决方法,那将非常有用。

TIA

0 个答案:

没有答案