Home > Software design >  Join with OneToOneField relathionship django
Join with OneToOneField relathionship django

Time:04-28

I have two models with OneToOneField relationship and I want to somehow join them and be able to access the attribute of the one model from the other.

My models.py is as follows:

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))

My views.py is as follows:

@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)

Right now the query is as follows:

[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)])]

Is it possible somehow to agument it with the correspondent created_at field from Event?

[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)])]

CodePudding user response:

One solution is to add serializer's method field to your 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 "-"

and after this, created_at will appear in your serialized data.

  • Related