加入 OneToOneField 关系 django

Join with OneToOneField relathionship django

我有两个具有 OneToOneField 关系的模型,我想以某种方式加入它们并能够从另一个模型访问一个模型的属性。

我的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)
    created_at = models.DateTimeField(auto_now_add=True, 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))

我的views.py如下:

@api_view(['GET'])
def result(request):

    results_list = Result.objects.all()
    num_results_list = Result.objects.all().count()
    if num_results_list < RES_LIMIT:
        serializer = ResultSerializer(results_list, many=True)
        query = serializer.data
        return Response(query)
    else:
        results_list = Result.objects.order_by('-created_at')[:RES_LIMIT]
        serializer = ResultSerializer(results_list, many=True)
        query = serializer.data
        return Response(query)

现在query如下:

[OrderedDict([('id', 1), ('HVAC_flex', [49.0, 27.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]), ('DHW_flex', [4.0, 0.0, 45.0, 4.0, 20.0, 33.0, 42.0, 13.0]), ('lights_flex', [6.0, 8.0, 0.0, 0.0, 0.0, 18.0, 28.0, 0.0]), ('event_id_result', None)]), OrderedDict([('id', 2), ('HVAC_flex', [0.0, 0.0, 15.0, 0.0, 23.0, 6.0, 0.0, 0.0]), ('DHW_flex', [1.0, 2.0, 7.0, 47.0, 1.0, 19.0, 37.0, 9.0]), ('lights_flex', [40.0, 28.0, 34.0, 6.0, 8.0, 43.0, 6.0, 0.0]), ('event_id_result', None)]), OrderedDict([('id', 3), ('HVAC_flex', [22.0, 19.0, 0.0, 6.0, 15.0, 12.0, 13.0, 0.0]), ('DHW_flex', [0.0, 21.0, 23.0, 18.0, 29.0, 31.0, 10.0, 19.0]), ('lights_flex', [0.0, 48.0, 11.0, 36.0, 18.0, 5.0, 31.0, 44.0]), ('event_id_result', None)])]

是否可以通过 Event 中对应的 created_at 字段以某种方式对其进行论证?

[OrderedDict([('id', 1), ('created_at', '19-04-2022 15:12:43'), ('HVAC_flex', [49.0, 27.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]), ('DHW_flex', [4.0, 0.0, 45.0, 4.0, 20.0, 33.0, 42.0, 13.0]), ('lights_flex', [6.0, 8.0, 0.0, 0.0, 0.0, 18.0, 28.0, 0.0]), ('event_id_result', None)]), OrderedDict([('id', 2), ('created_at', '19-04-2022 15:12:43'), ('HVAC_flex', [0.0, 0.0, 15.0, 0.0, 23.0, 6.0, 0.0, 0.0]), ('DHW_flex', [1.0, 2.0, 7.0, 47.0, 1.0, 19.0, 37.0, 9.0]), ('lights_flex', [40.0, 28.0, 34.0, 6.0, 8.0, 43.0, 6.0, 0.0]), ('event_id_result', None)])]

一种解决方案是将序列化程序的方法字段添加到您的 ResultSerializer:

class ResultSerializer(serializers.ModelSerializer):
    created_at = serializers.SerializerMethodField()

    # some other parts of your serializer which I don't know right now...
    class Meta:
        fields = ["some_fields", ..., "created_at"]
        model = Result
    
    @staticmethod
    def get_created_at(obj):
        return obj.event_id_result.created_at if obj.event_id_result else "-"

之后,created_at 将出现在您的序列化数据中。