多对多

在django中的多对多是指一个表可以关联很多表

多对多的字段是    ManyToManyField  

关联两张表的时候用    to

两张表使用多对多的时候会生成第三张表,第三张表是拿两张表的关系

多对多查询的时候用序列化,要用到第一张表的序列化,需要写入many=True的约束

在序列化时添加这个字段

class RuleSer(serializers.ModelSerializer):
    class Meta:
        model = Rule
        fields = '__all__'


class RoleSer(serializers.Serializer):
    id = serializers.IntegerField()
    name = serializers.CharField(max_length=99)
    static = serializers.BooleanField(default=False)
    rule = RuleSer(many=True)
    class Meta:
        model = Role
        fields = ('id', 'name', 'static', 'rule')

 

posted @ 2020-03-10 21:23  尚尚123  阅读(245)  评论(0编辑  收藏  举报