Django restframework 嵌套关系处理
场景:带有外键的model,且是始终一对一创建的关系。
model.py:
class Album(models.Model): album_name = models.CharField(verbose_name=u'名称', max_length=100) artist = models.CharField(max_length=100) class Meta: db_table = 'album' verbose_name = u'album表' class Track(models.Model): album = models.ForeignKey(Album, related_name='tracks') order = models.IntegerField() title = models.CharField(max_length=100) duration = models.IntegerField() class Meta: db_table = 'track'
序列化serializer.py如下:
class AlbumSerializer(serializers.ModelSerializer): class Meta: model = Album fields = ('id', 'album_name', 'artist') class TrackSerializer(serializers.ModelSerializer): album = AlbumSerializer() class Meta: model = Track fields = ('id', 'order', 'title', 'duration', 'album') depth = 1 def create(self, validated_data): print validated_data album_data = validated_data.pop('album') album = Album.objects.create(**album_data) validated_data['album'] = album track = Track.objects.create(**validated_data) return track
这里有四点需要注意:
1、album = AlbumSerializer() 表示Album里的字段为必填,若不是必填required=False
2、如果嵌套表示应该是项列表,则应将该many=True
标志传递给嵌套序列化
3、create()、update()方法必须重写。默认值ModelSerializer
.create()
和.update()
方法不包括对可写嵌套表示的支持。
4、参数的组装形式如下:
{ "order": null, "title": "", "duration": null, "album": { "album_name": "", "artist": "" } }
views.py:
class AlbumViewSet(viewsets.ModelViewSet): queryset = Album.objects.all() serializer_class = AlbumSerializer class TrackViewSet(viewsets.ModelViewSet): queryset = Track.objects.all() serializer_class = TrackSerializer
urls.py:
router = routers.DefaultRouter(trailing_slash=True) router.register(r'album', views.AlbumViewSet, base_name='album') router.register(r'track', views.TrackViewSet, base_name='track')
运行:http://127.0.0.1:8000/track/,页面如下: