我有和Exercise
的模型。Training
Workout
Training
包含一些练习 ( Exercise
)
Workout
包含培训 ( Training
)。
我的models.py的片段:
class Exercise(models.Model):
user = models.ForeignKey(User, related_name='exercises',
on_delete=models.CASCADE)
name = models.CharField(max_length=80)
description = models.TextField(max_length=300)
details = models.ManyToManyField(ExerciseDetail, blank=True)
...
class Training(models.Model):
user = models.ForeignKey(User, related_name='trainings',
on_delete=models.CASCADE)
name = models.CharField(max_length=80)
description = models.CharField(max_length=250)
exercises = models.ManyToManyField(Exercise, related_name='trainings',
blank=True)
...
class Workout(models.Model):
user = models.ForeignKey(User, related_name='workouts',
on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True)
name = models.CharField(max_length=200)
description = models.TextField(max_length=400, blank=True)
trainings = models.ManyToManyField(Training, related_name='workouts',
blank=True)
...
我希望有可能使用类似的东西Workout.objects.get(name='workout').exercises.objects.all()
来获取包含在所选锻炼训练中的所有练习的列表/集。
我也想有可能使用exercises`` field with Django Rest Framework to list all exercises, possibly with link to particular
练习```模型序列化程序。
有人可以提示我该怎么做吗?