我正在转换一些数据以使用DRF。我有一个POST端点,它接受JSON有效负载,解析相关字段,并从该数据中创建多个对象。创建的每个后续对象也依赖于先前创建的对象(请参见下面的示例)。我试图找出如何在下面完成,使用DRF反序列化数据。它不一定要像下面的代码那样建模,但我需要相同的最终结果。
def job_start(request, plat_name="other", script_version="1"):
a_product, created = Product.objects.get_or_create(name="Mobile")
a_platform, created = Platform.objects.get_or_create(name=plat_name)
tool, created = Tool.objects.get_or_create(
product=a_product,
platform=a_platform,
url=jenkins_dump.url)
job, created = Job.objects.get_or_create(name=jenkins_dump.job_name, tool=tool)
job_in_progress, _ = Status.objects.get_or_create(name="IN_PROGRESS")
start_time_raw = datetime.fromtimestamp(jenkins_dump.start_time / 1000)
job_execution = JobExecution.objects.create(build_id=jenkins_dump.execution_number,
job=job,
time_start=start_time_raw,
status=job_in_progress,
pipeline_script_version=script_version)
# what I want to do
# serializer = ProductSerializer(request.data)
# if serializer.is_valid():
# serializer.save()
# serializer = ToolSerializer(request.data)
# if serializer.is_valid():
# serializer.save()
我想知道如何序列化所有这些值,然后在创建对象时,将其附加到序列化数据,然后将该序列化数据传递给下一个对象序列化器等。