简体   繁体   English

如何将数据从一个模型字段处理到另一个模型字段

[英]How to process data from one model field to another

I have models of Exercise , Training and Workout .我有ExerciseTrainingWorkout

Training contains some exercises ( Exercise ) Training包含一些练习( Exercise

Workout contains trainings ( Training ). Workout包含培训 ( Training )。

Snippet of my models.py:我的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)
...

I would like to have possibility to use something like Workout.objects.get(name='workout').exercises.objects.all() to get a list/set of all exercises included in trainings of chosen Workout.我想有可能使用Workout.objects.get(name='workout').exercises.objects.all()来获取所选锻炼训练中包含的所有练习的列表/集。

I would also like to have possibility to use exercises`` field with Django Rest Framework to list all exercises, possibly with link to particular Exercise``` model serializer.我还希望有可能在exercises`` field with Django Rest Framework to list all exercises, possibly with link to particular使用exercises`` field with Django Rest Framework to list all exercises, possibly with link to particular练习```模型序列化程序的exercises`` field with Django Rest Framework to list all exercises, possibly with link to particular

Can someone give a hint how can I do that?有人可以提示我该怎么做吗?

You can query this with:您可以通过以下方式查询:

Exercise.objects.filter(
    trainings__workouts__name='workout'
)

With the consecutive underscores ( __ ), you thus can look "through" relations.使用连续的下划线 ( __ ),您可以“透视”关系。

This will thus return the Exercise s that belong to Training s that belong to Workout s with as name 'Workout' .因此,这将返回属于Training s 的Exercise s 属于名为'Workout' Workout s。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字段值从一个模型获取到另一个模型 - How to get a field value from one model to another model 如何将字段从一个模型移动到另一个模型,并仍然保留数据? - How can I move a field from one model to another, and still retain the data? 如何使用一个模型中的字段信息来计算其他模型中的另一个字段? - How can I use the information of a field from one model to calculate another field in other model? 如何将一个模型的字段值分配给另一个模型的字段? - How to assign value of field of one model to field of another model? 如何使用继承自AbstractUser的模型从具有一对一依赖关系的模型中的表单字段中保存数据 - How to save data from form field in Model with One to One reltionship with a Model inheriting from AbstractUser 如何根据Django中一个模型的计算重新计算另一个模型中的字段? - How to recalculate a field in another model based on the calculations of one model in Django? Django:如何根据来自行的数据和来自另一个 Model 的数据将聚合字段添加到查询集中? - Django: How can I add an aggregated field to a queryset based on data from the row and data from another Model? 如何在Python中停止一个进程 - how to stop one process from another in Python 如何将异常从一个进程传递到另一个进程? - How to pass exception from one process to another? 如何从一个过程向另一个过程发出信号? - How to signal from one process to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM