我实现了django rest框架并收到了以下结果:
{
"count": 3,
"next": null,
"previous": null,
"results": [
{
"id": 2,
"created_at": "2017-10-16T09:55:07.663791Z",
"modified_at": "2017-10-16T09:55:07.663846Z",
"visible": true,
"name": "Viewer",
"permissions": [
2,
3
]
},
...
]
}
所以我希望字段权限为:
"permissions": [
"name permission 1",
"name permission 2"
]
我该怎么办?
编辑:
我添加了我的模型和序列化代码:
作用:
class Role(BaseModel):
class Meta:
db_table = 'role'
name = models.CharField(max_length=255, null=False)
module = models.CharField(max_length=30)
permissions = models.ManyToManyField(Permission,
related_name='role_permission',
through='RolePermission', )
def __str__(self):
return self.name
RolePermission:
class RolePermission(models.Model):
class Meta:
db_table = 'role_permission'
unique_together = (('role', 'permission'),)
role = models.ForeignKey(Role)
permission = models.ForeignKey(Permission)
RoleSerializer
class RoleSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = '__all__'
答案 0 :(得分:0)
您可以修改RoleSerializer
来实现此目的,以及Permission
模型的新序列化程序类。
class PermissionSerializer(serializers.ModelSerializer):
class Meta(object):
model = Permission
fields = ('name', )
class RoleSerializer(serializers.ModelSerializer):
permissions = PermissionSerializer(many=True)
class Meta:
model = Role
fields = '__all__'