@@ -628,23 +628,37 @@ class Meta:
628628 "translation_module_skip_auto_translation" ,
629629 )
630630
631+ @staticmethod
632+ def _get_latest_prefetched_id (obj , attr_name , model_cls , filter_kwargs ) -> typing .Optional [int ]:
633+ prefetched = getattr (obj , attr_name , None )
634+ if prefetched is not None :
635+ return prefetched [0 ].id if prefetched else None
636+ instance = model_cls .objects .filter (** filter_kwargs ).order_by ("-id" ).first ()
637+ return instance .id if instance else None
638+
631639 def get_assessment (self , obj ) -> typing .Optional [int ]:
632- assessment = PerAssessment .objects .filter (overview = obj ).last ()
633- if assessment :
634- return assessment .id
635- return None
640+ return self ._get_latest_prefetched_id (
641+ obj ,
642+ "latest_perassessments" ,
643+ PerAssessment ,
644+ {"overview" : obj },
645+ )
636646
637647 def get_prioritization (self , obj ) -> typing .Optional [int ]:
638- prioritization = FormPrioritization .objects .filter (overview = obj ).last ()
639- if prioritization :
640- return prioritization .id
641- return None
648+ return self ._get_latest_prefetched_id (
649+ obj ,
650+ "latest_prioritizations" ,
651+ FormPrioritization ,
652+ {"overview" : obj },
653+ )
642654
643655 def get_workplan (self , obj ) -> typing .Optional [int ]:
644- workplan = PerWorkPlan .objects .filter (overview = obj ).last ()
645- if workplan :
646- return workplan .id
647- return None
656+ return self ._get_latest_prefetched_id (
657+ obj ,
658+ "latest_workplans" ,
659+ PerWorkPlan ,
660+ {"overview" : obj },
661+ )
648662
649663
650664class PublicPerProcessSerializer (serializers .ModelSerializer ):
@@ -677,23 +691,37 @@ class Meta:
677691 "ns_focal_point_email" ,
678692 )
679693
694+ @staticmethod
695+ def _get_latest_prefetched_id (obj , attr_name , model_cls , filter_kwargs ) -> typing .Optional [int ]:
696+ prefetched = getattr (obj , attr_name , None )
697+ if prefetched is not None :
698+ return prefetched [0 ].id if prefetched else None
699+ instance = model_cls .objects .filter (** filter_kwargs ).order_by ("-id" ).first ()
700+ return instance .id if instance else None
701+
680702 def get_assessment (self , obj ) -> typing .Optional [int ]:
681- assessment = PerAssessment .objects .filter (overview = obj ).last ()
682- if assessment :
683- return assessment .id
684- return None
703+ return self ._get_latest_prefetched_id (
704+ obj ,
705+ "latest_perassessments" ,
706+ PerAssessment ,
707+ {"overview" : obj },
708+ )
685709
686710 def get_prioritization (self , obj ) -> typing .Optional [int ]:
687- prioritization = FormPrioritization .objects .filter (overview = obj ).last ()
688- if prioritization :
689- return prioritization .id
690- return None
711+ return self ._get_latest_prefetched_id (
712+ obj ,
713+ "latest_prioritizations" ,
714+ FormPrioritization ,
715+ {"overview" : obj },
716+ )
691717
692718 def get_workplan (self , obj ) -> typing .Optional [int ]:
693- workplan = PerWorkPlan .objects .filter (overview = obj ).last ()
694- if workplan :
695- return workplan .id
696- return None
719+ return self ._get_latest_prefetched_id (
720+ obj ,
721+ "latest_workplans" ,
722+ PerWorkPlan ,
723+ {"overview" : obj },
724+ )
697725
698726
699727class QuestionResponsesSerializer (
0 commit comments