serialization - Django Rest Framework Serializer format -


i have 2 serializers: 1 restaurant model, mainmenu model:

class restaurantserializer(serializers.modelserializer):     class meta:         model = restaurant   class mainmenuserializer(serializers.modelserializer):      restaurant = restaurantserializer()      main_menu_items = serializers.stringrelatedfield(many=true)      class meta:         model = menumain         fields = ('id', 'restaurant', 'main_menu_items') 

the current output of mainmenuserializer is

[     {         "id": 1,         "restaurant": {             "id": 1,             "name": "restaurant a",             "location": "street b"         },         "main_menu_items": [             "fried rice"         ]     },     {         "id": 2,         "restaurant": {             "id": 1,             "name": "restaurant a",             "location": "street b",         },         "main_menu_items": [             "noodles"         ]     } ] 

but want restaurantserializer output once, this:

[     {         "restaurant": {             "id": 1,             "name": "restaurant a",             "location": "street b"         }     },     [         {             "id": 1,             "main_menu_items": [                 "fried rice",                 "meat balls"             ]         },         {             "id": 2,             "main_menu_items": [                 "noodles"             ]         }     ] ] 

edit: models used

class restaurant(models.model):     name = models.charfield(max_length=100, default='')     location  = models.charfield(max_length=100, default='')  class menumain(models.model):     price = models.integerfield()     restaurant = models.foreignkey(restaurant, related_name='main_menus')  class menumainitems(models.model):     menu_main = models.foreignkey(menumain, related_name='main_menu_items')     item = models.charfield(max_length=150, default='') 

the more "django rest" way of doing set url node each restaurant returns menu items in restaurant. instance,

urls.py

url(r'restaurant-menu/(?p<pk>[0-9]+)$', menuitemviewset.as_view()) 

in viewset class

class menuitemviewset(viewsets.modelviewset):     serializer_class = mainmenuserializer      def retrieve(self, request, pk=none):         return restaurant.objects.get(pk=pk).menumain_set.all()[0].menumainitems_set.all() 

this of course assumes 1 menu per restaurant. if there multiple menus , want of them, it's best practice break 2 calls: 1 restaurant's menu, , particular menu's items. otherwise many assumptions being made organization of data, , that's rather fragile design pattern.

link docs on fetching related objects

if still intent on getting response asked for, need reverse serializer nesting, i.e. add menu field restaurantserializer.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -