无法正确保存到具有 OneToOne 关系的数据库

Cannot properly save to a db with OneToOne relationship

我想做的流程如下:

我有两个table,即EventResultEvent table 与 ForeignKey 与用户关联,Result table 与 OneToOneEvent 关联。在我的 views.py 中,我有一个 POST 和一个 GET。在 POST 中,我取了一些数据并将其保存在 table Event 中。在 GET 中,我解决了一个优化问题,我想将解决方案保存到 Result

models.py如下:

from django.db import models
from django.contrib.postgres.fields import ArrayField
from django.contrib.auth import get_user_model

CustomUser = get_user_model()

class Event(models.Model):
    
    user_id_event = models.ForeignKey(CustomUser, on_delete=models.CASCADE, null=True)
    dr_notice_period = models.IntegerField(blank=True, null=True)
    dr_duration = models.IntegerField(blank=True, null=True)
    dr_request = models.FloatField(blank=True, null=True)

class Result(models.Model):

    event_id_result = models.OneToOneField(Event, on_delete=models.CASCADE, null=True)
    HVAC_flex = ArrayField(models.FloatField(blank=True, null=True))
    DHW_flex = ArrayField(models.FloatField(blank=True, null=True))
    lights_flex = ArrayField(models.FloatField(blank=True, null=True))

serializers.py如下:

from rest_framework import serializers
from vpp_optimization.models import Event, Result

class EventSerializer(serializers.ModelSerializer):

    class Meta:
        model = Event
        fields = ('__all__')

class ResultSerializer(serializers.ModelSerializer):
    HVAC_flex = serializers.ListField(child=serializers.FloatField())
    DHW_flex = serializers.ListField(child=serializers.FloatField())
    lights_flex = serializers.ListField(child=serializers.FloatField())

    class Meta:
        model = Result
        fields = ('__all__')

views.py如下:

from rest_framework.response import Response
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework import status

from vpp_optimization.importer import DR_event_import_data
from vpp_optimization.utils import optimization_solution, convert_dict_for_serializer
from vpp_optimization.serializers import EventSerializer, ResultSerializer
from vpp_optimization.models import Event, Result

@api_view(['POST'])
@permission_classes([IsAuthenticated,])
def event(request):
    serializer = EventSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save(user_id_event=request.user)
        return Response({"status": "success", "data": serializer.data}, status=status.HTTP_200_OK)
    else:
        return Response({"status": "error", "data": serializer.errors}, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET'])
def optimization(request):
    last_event = Event.objects.last()

    if not last_event:
        return Response({"res": "Object Event does not exists"}, status=status.HTTP_400_BAD_REQUEST)

    serializer = EventSerializer(last_event)
    
    response_dict = {}
    DR_event_data = DR_event_import_data(serializer.data)
    response_dict = optimization_solution(DR_event_data)
    response_dict_final = convert_dict_for_serializer(response_dict)

    serializer_result = ResultSerializer(data=response_dict_final)

    if serializer_result.is_valid():
        serializer_result.save(event_id_result=last_event.id)
    
    return Response(serializer_result.data, status=status.HTTP_200_OK)

但是,我收到以下错误:

ValueError: Cannot assign "2": "Result.event_id_result" must be a "Event" instance.

错误是由于我试图用 OneToOne 关系保存的方式,即 views.py:

的这一行
event_id_result=last_event.id

显然,这不是连接两个数据库的正确方法。谁能帮我找到正确的方法?

变化:

event_id_result=last_event.id

收件人:

event_id_result=last_event