json - django rest framework view with merged results from different object serializers -
having following models
class treelifephase(dbordered): name = models.charfield(max_length=200) def __unicode__(self): return self.name class treewidth(dbordered): name = models.charfield(max_length=200) def __unicode__(self): return self.name
and many more this, contain editable attributes of tree
objects. select field on ui want have available treelifephases , treewidths 1 query - have json result looks like
{ "treelifephases": [ { "id": 1, "name": "young" }, { "id": 2, "name": "medium" }, { "id": 3, "name": "old" } ], "treewidths": [ { "id": 1, "name": "10-20cm" }, { "id": 2, "name": "21-30cm" }, { "id": 3, "name": "31-40cm" } ] }
i have serializers models @ hand , awesome have view list of serializers return resultset above.
you can use serializermethodfield
, need create model, in order use model serializer.
class fulltree(serializers.modelserializer): full_tree = serializers.serializermethodfield() class meta: model = fulltreemodel fields = ('treewidth','lifephase') get_full_tree(obj): treewidth = obj.treewidths # treewidth lifephase = obj.lifephases# lifephases //build tree serializers return your_full_tree
Comments
Post a Comment