@@ -208,15 +208,25 @@ class MiniDrefSerializer(serializers.ModelSerializer):
208208 type_of_dref_display = serializers .CharField (source = "get_type_of_dref_display" , read_only = True )
209209 status_display = serializers .CharField (source = "get_status_display" , read_only = True )
210210 country_details = MiniCountrySerializer (source = "country" , read_only = True )
211- has_ops_update = serializers .SerializerMethodField ( )
212- has_final_report = serializers . SerializerMethodField ()
211+ starting_language = serializers .CharField ( read_only = True )
212+ # Manually added fields
213213 application_type = serializers .SerializerMethodField ()
214214 application_type_display = serializers .SerializerMethodField ()
215- unpublished_op_update_count = serializers .SerializerMethodField ()
216- unpublished_final_report_count = serializers .SerializerMethodField ()
217- operational_update_details = serializers .SerializerMethodField ()
218- final_report_details = serializers .SerializerMethodField ()
219- starting_language = serializers .CharField (read_only = True )
215+ # Annotate values from queryset
216+ has_ops_update = serializers .BooleanField (read_only = True )
217+ has_final_report = serializers .BooleanField (read_only = True )
218+ unpublished_op_update_count = serializers .IntegerField (read_only = True )
219+ unpublished_final_report_count = serializers .IntegerField (read_only = True )
220+ # Prefetched data
221+ operational_update_details = MiniOperationalUpdateActiveSerializer (
222+ source = "prefetched_operational_updates" ,
223+ many = True ,
224+ read_only = True ,
225+ )
226+ final_report_details = MiniDrefFinalReportActiveSerializer (
227+ source = "dreffinalreport" ,
228+ read_only = True ,
229+ )
220230
221231 class Meta :
222232 model = Dref
@@ -250,39 +260,14 @@ class Meta:
250260 "starting_language" ,
251261 ]
252262
253- @extend_schema_field (MiniOperationalUpdateActiveSerializer (many = True ))
254- def get_operational_update_details (self , obj ):
255- queryset = DrefOperationalUpdate .objects .filter (dref_id = obj .id ).order_by ("-created_at" )
256- return MiniOperationalUpdateActiveSerializer (queryset , many = True ).data
257-
258- @extend_schema_field (MiniDrefFinalReportActiveSerializer )
259- def get_final_report_details (self , obj ):
260- queryset = DrefFinalReport .objects .filter (dref_id = obj .id ).first ()
261- return MiniDrefFinalReportActiveSerializer (queryset ).data
262-
263- def get_has_ops_update (self , obj ) -> bool :
264- op_count_count = obj .drefoperationalupdate_set .count ()
265- if op_count_count > 0 :
266- return True
267- return False
268-
269- def get_has_final_report (self , obj ) -> bool :
270- if hasattr (obj , "dreffinalreport" ):
271- return True
272- return False
273-
274- def get_application_type (self , obj ) -> str :
263+ @extend_schema_field (serializers .CharField ())
264+ def get_application_type (self , _ ) -> str :
275265 return "DREF"
276266
277- def get_application_type_display (self , obj ) -> str :
267+ @extend_schema_field (serializers .CharField ())
268+ def get_application_type_display (self , _ ) -> str :
278269 return gettext ("DREF application" )
279270
280- def get_unpublished_op_update_count (self , obj ) -> int :
281- return DrefOperationalUpdate .objects .filter (dref_id = obj .id ).exclude (status = Dref .Status .APPROVED ).count ()
282-
283- def get_unpublished_final_report_count (self , obj ) -> int :
284- return DrefFinalReport .objects .filter (dref_id = obj .id ).exclude (status = Dref .Status .APPROVED ).count ()
285-
286271
287272class PlannedInterventionSerializer (
288273 NestedCreateMixin ,
0 commit comments