drf-mongoengine DynamicDocumentSerializer不包含嵌入字段

时间:2016-06-14 09:18:28

标签: django django-rest-framework mongoengine

我正在使用

  

django-rest-framework-mongoengine == 3.3.0

     

mongoengine == 0.10.5

field1, field2, field3

这是序列化器

field4

但在我的api中只返回class myListAPI(generics.ListAPIView): model = MyModel serializer_class = MyModelSerializer queryset = MyModel.objects.all() 。甚至在明确提到gulp.task('unused', function() { return syncronize({ src: 'app/coffee/', //Source folder target: 'app/js/', //Output folder extensions: { 'js': 'coffee', // Corresponds .js files to the .coffee files; } }); });

之后
router.put('/getProfile/:profile_id/addWin'), function (req, res) {
UserProfile.findOne({
    UserID : req.params.profile_id //Find the correct Profile
}, function (err, profile) {
    if (err)
        res.send(err);

    profile.Drafts.findByID({ //Find the corrct Draft in the Profile
        _id : req.body.DraftID
    }, function (err, draft) {

        if (err)
            res.send(err);

        draft.Wins += 1; //Increment the wins
    })

    profile.save(function (err) { //Save the profile
        if (err)
            res.send(err);

        res.json({
            message : 'Win added!'
        });
    });
});}

1 个答案:

答案 0 :(得分:1)

好吧,你必须为它创建一个单独的序列化程序并包含在主序列化程序中

创建Model2序列化程序

class MyModel2Serializer(DynamicDocumentSerializer):
    class Meta:
        model = MyModel2
        fields = ('sn',)

并包含在序列化程序中

class MyModelSerializer(DynamicDocumentSerializer):
    field4 = MyModel2Serializer()
    class Meta:
        model = MyModel
        fields = ('field1', 'field4')