serialization - Recursive many-to-many relationship with Django RestFramework -
i want make recursive many many relationship. have games , each game promote games. , each game got list of assets. i'm using django , django restframework.
my models.py
class game(models.model): name = models.charfield(max_length=30, unique=true, blank=false, null=false) promote_games = models.manytomanyfield("self", through='gamepromoted', symmetrical=false, ) class meta: db_table = 'game' class gamepromoted(models.model): game = models.foreignkey(game, related_name='games_promoted') game_promoted = models.foreignkey(game, related_name='games') class meta: db_table = 'game_promoted' unique_together = ('game', 'game_promoted')
and serializers.py
class gamepromotedserializer(serializers.modelserializer): ??????? class meta: model = gamepromoted fields = ('id', 'game_id', 'name', 'assets') class gameserializer(serializers.modelserializer): assets = assetserializer(many=true, read_only=true) games_promoted = gamepromotedserializer(many=true, read_only=true) class meta: model = game fields = ('id', 'name', 'assets', 'games_promoted')
and want kind of result /games/1
{ "id": 1, "name": "game1", "assets": [{...}], "games_promoted": [ { "id": 1, "game_id": 2, "name": "game2" "assets": [{...}] }, { "id": 2, "game_id": 3, "name": "game3" "assets": [{...}] } ] }
and want add game promote in game post /games/1/ (game_id=1&game_promoted_id=5) exemple
so i'm stuck in serializers.py, think models good. help.
Comments
Post a Comment