DRf,分页外键字段

时间:2019-09-21 02:37:16

标签: django django-rest-framework

我想做下面的事情,但是代码效率低下,

如何返回相关对象的分页响应?

System.out.println("Title: ");  
               String i = in.nextLine();
                title.add(i);
            try{ 




            FileWriter fw = new FileWriter("C:\\title.txt");
            for(String de: title){
                fw.write(de + System.lineSeparator()); 

            }
            fw.close();

        }catch(Exception e){ System.out.println(e);}
            System.out.println("Sucess..");
        }

1 个答案:

答案 0 :(得分:0)

实际上我在这里https://stackoverflow.com/a/37657376/433570

找到了答案

想法是平整嵌套对象

class FooSerializer():
    bar = BarSerializer()

    def to_representation(self, obj):
        """Move fields from profile to user representation."""
        representation = super().to_representation(obj)
        profile_representation = representation.pop('bar')
        for key in profile_representation:
            representation[key] = profile_representation[key]

        return representation