Django фильтрует подэлементы в одном запросе

У меня есть решение, которое отлично сочетается с bodyParser, используя обратный вызов verify в bodyParser. В этом коде я использую его, чтобы получить sha1 содержимого, а также получить сырое тело.

app.use(bodyParser.json({
    verify: function(req, res, buf, encoding) {

        // sha1 content
        var hash = crypto.createHash('sha1');
        hash.update(buf);
        req.hasha = hash.digest('hex');
        console.log("hash", req.hasha);

        // get rawBody        
        req.rawBody = buf.toString();
        console.log("rawBody", req.rawBody);

    }
}));

Я новичок в Node.js и express.js (начался вчера, буквально!) поэтому я хотел бы услышать комментарии к этому решению.

0
задан directedition 17 January 2019 в 01:37
поделиться

1 ответ

Это можно сделать в сериализаторе с полем метода.

    students = SerializerMethodField(read_only=True)
    def get_students(self, obj):
        """
        Administration sees everyone, teachers see only verified.
        """

        if self.context['request'].user.type == 'teacher':
            qs = obj.students.filter(verified=True)
        elif self.context['request'].user.type == 'administration':
            qs = obj.students
        return qs
0
ответ дан directedition 17 January 2019 в 01:37
поделиться
Другие вопросы по тегам:

Похожие вопросы: