如何在 DRF 中使用 GET 请求获取包括关系 table 条目的数据
How to get the data including the relation table's entry with a GET request in DRF
我最近正在尝试 DRF,我的项目有以下问题需要解决。
我有两个 模特
class JobsMaster(models.Model):
job_id = models.CharField(max_length=128, unique=True)
job_name = models.CharField(max_length=128)
scheduled_time = models.CharField(max_length=128)
args = models.TextField()
interval = models.CharField(max_length=50)
repeat = models.CharField(max_length=100)
class JobProcessDetails(models.Model):
job_id = models.ForeignKey('JobsMaster')
sequence = models.IntegerField()
step_name = models.CharField(max_length=128)
records = models.IntegerField(null=True,blank=True)
created_by = models.CharField(max_length=155,default='system')
created_on = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = (('job_id', 'sequence'),)
相应的序列化程序
class JobsMasterSerializer(serializers.ModelSerializer):
class Meta:
model = JobsMaster
fields = '__all__'
class JobProcessDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = JobProcessDetails
fields = '__all__'
他们的视图集
class JobsMasterViewSet(viewsets.ModelViewSet):
queryset = JobsMaster.objects.all()
serializer_class = JobsMasterSerializer
filter_fields = ('job_id',)
class JobProcessDetailsViewSet(viewsets.ModelViewSet):
queryset = JobProcessDetails.objects.all()
serializer_class = JobProcessDetailsSerializer
filter_fields = ('job_id','sequence',)
我不知道这是否是构建视图集和序列化程序的正确方法,但它对我有用。
我想从 UI 写一个 GET 请求来从 JobProcessDetails
获取数据,其中还包括来自相关 JobsMaster
table 的数据。
例如,响应数据应包含以下字段
job_id : xxxxx
sequence : xxxx
step_name : 'xxxx'
records : xxxx
created_by : 'xxxxx'
created_on : dd/mm/yyyy
job_id__job_name : 'xxxxx'
job_id__scheduled_time : 'xxxxx'
job_id__args : 'xxxxx'
job_id__interval : 'xxxxx'
job_id__repeat : 'xxxxx'
有什么方法可以通过单个 GET 请求调整某些内容。
这样试试
class JobProcessDetailsSerializer(serializers.ModelSerializer):
job_id = JobsMasterSerializer(read_only=True)
class Meta:
model = JobProcessDetails
fields = [
'job_id '
'sequence'
'step_name'
'records'
'created_by'
'created_on'
]
我最近正在尝试 DRF,我的项目有以下问题需要解决。
我有两个 模特
class JobsMaster(models.Model):
job_id = models.CharField(max_length=128, unique=True)
job_name = models.CharField(max_length=128)
scheduled_time = models.CharField(max_length=128)
args = models.TextField()
interval = models.CharField(max_length=50)
repeat = models.CharField(max_length=100)
class JobProcessDetails(models.Model):
job_id = models.ForeignKey('JobsMaster')
sequence = models.IntegerField()
step_name = models.CharField(max_length=128)
records = models.IntegerField(null=True,blank=True)
created_by = models.CharField(max_length=155,default='system')
created_on = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = (('job_id', 'sequence'),)
相应的序列化程序
class JobsMasterSerializer(serializers.ModelSerializer):
class Meta:
model = JobsMaster
fields = '__all__'
class JobProcessDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = JobProcessDetails
fields = '__all__'
他们的视图集
class JobsMasterViewSet(viewsets.ModelViewSet):
queryset = JobsMaster.objects.all()
serializer_class = JobsMasterSerializer
filter_fields = ('job_id',)
class JobProcessDetailsViewSet(viewsets.ModelViewSet):
queryset = JobProcessDetails.objects.all()
serializer_class = JobProcessDetailsSerializer
filter_fields = ('job_id','sequence',)
我不知道这是否是构建视图集和序列化程序的正确方法,但它对我有用。
我想从 UI 写一个 GET 请求来从 JobProcessDetails
获取数据,其中还包括来自相关 JobsMaster
table 的数据。
例如,响应数据应包含以下字段
job_id : xxxxx
sequence : xxxx
step_name : 'xxxx'
records : xxxx
created_by : 'xxxxx'
created_on : dd/mm/yyyy
job_id__job_name : 'xxxxx'
job_id__scheduled_time : 'xxxxx'
job_id__args : 'xxxxx'
job_id__interval : 'xxxxx'
job_id__repeat : 'xxxxx'
有什么方法可以通过单个 GET 请求调整某些内容。
这样试试
class JobProcessDetailsSerializer(serializers.ModelSerializer):
job_id = JobsMasterSerializer(read_only=True)
class Meta:
model = JobProcessDetails
fields = [
'job_id '
'sequence'
'step_name'
'records'
'created_by'
'created_on'
]