Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. class ExerciseRoutineSerializer(serializers.ModelSerializer):
  2. exercise = ExerciseSerializer()
  3.  
  4. class Meta:
  5. model = ExerciseRoutine
  6. fields = '__all__'
  7.  
  8. def to_representation(self, obj):
  9. representation = super().to_representation(obj)
  10. exercise_representation = representation.pop('exercise')
  11. for key in exercise_representation:
  12. representation[key] = exercise_representation[key]
  13.  
  14. return representation
  15.  
  16. class RoutineSerializer(serializers.ModelSerializer):
  17. exercises = ExerciseRoutineSerializer(many=True)
  18.  
  19. class Meta:
  20. model = Routine
  21. fields = '__all__'
  22.  
  23. [
  24. {
  25. "id": 1,
  26. "routines": [
  27. {
  28. "id": 1,
  29. "exercises": [
  30. {
  31. "id": 1,
  32. "index": 0,
  33. "rest_time": 60,
  34. "repetition_goal": 10,
  35. "equipment": [
  36. {
  37. "id": 1,
  38. "equipment": "Fixed Bar"
  39. }
  40. ],
  41. "name": "Pull up"
  42. }
  43. ],
  44. "name": "RoutineName",
  45. "routine_type": "Regular"
  46. }
  47. ],
  48. "name": "WorkoutName"
  49. }
  50. ]
  51.  
  52. [
  53. {
  54. "id": 1,
  55. "routines": [
  56. {
  57. "id": 1,
  58. "exercises": {
  59. 0 : {"id": 1, "rest_time": 60, "repetition_goal": 10,"equipment": [{..}], "name": "Pull up"},
  60. 1 : {"id": 13, "rest_time": 60, "repetition_goal": 10,"equipment": [{..}], "name": "Anotherexericse"}
  61. },
  62. "name": "RoutineName",
  63. "routine_type": "Regular"
  64.  
  65. }
  66. ],
  67. "name": "WorkoutName"
  68. }
  69. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement