@ -3798,24 +3798,18 @@ class AdminBookEditionSerializer(serializers.ModelSerializer):
def to_representation ( self , instance ) :
def to_representation ( self , instance ) :
ret = super ( ) . to_representation ( instance )
ret = super ( ) . to_representation ( instance )
res = instance . researchers . first ( )
# Use prefetched relations (no extra queries per row)
researchers = list ( instance . researchers . all ( ) )
editors = list ( instance . editors . all ( ) )
res = researchers [ 0 ] if researchers else None
ret [ " researcher_author_id " ] = res . author_id if res and res . author_id else None
ret [ " researcher_author_id " ] = res . author_id if res and res . author_id else None
ed_obj = instance . editors . first ( )
ed_obj = editors [ 0 ] if editors else None
ret [ " editor_author_id " ] = ed_obj . author_id if ed_obj and ed_obj . author_id else None
ret [ " editor_author_id " ] = ed_obj . author_id if ed_obj and ed_obj . author_id else None
ret [ " books " ] = list ( instance . books . values_list ( ' id ' , flat = True ) )
ret [ " books_detail " ] = [
{
" id " : b . id ,
" title " : b . title ,
" slug " : b . slug ,
}
for b in instance . books . all ( )
]
ret [ " books " ] = [ b . id for b in instance . books . all ( ) ]
return ret
return ret
def get_number_of_volumes ( self , obj ) :
def get_number_of_volumes ( self , obj ) :
return obj . volumes . count ( )
return len ( obj . volumes . all ( ) )
def get_books_detail ( self , obj ) :
def get_books_detail ( self , obj ) :
return [
return [
@ -3920,10 +3914,6 @@ class AdminBookEditionSerializer(serializers.ModelSerializer):
# Delete editors no longer in the request payload
# Delete editors no longer in the request payload
keep_ids = [ e . get ( " id " ) for e in editors_data if e . get ( " id " ) ]
keep_ids = [ e . get ( " id " ) for e in editors_data if e . get ( " id " ) ]
instance . editors . exclude ( id__in = keep_ids ) . delete ( )
instance . editors . exclude ( id__in = keep_ids ) . delete ( )
if books_data is not None :
edition . books . set ( books_data )
if edition . book_reference and not edition . books . filter ( id = edition . book_reference_id ) . exists ( ) :
edition . books . add ( edition . book_reference )
for ed_data in editors_data :
for ed_data in editors_data :
ed_id = ed_data . get ( " id " )
ed_id = ed_data . get ( " id " )
if ed_id :
if ed_id :
@ -3937,6 +3927,16 @@ class AdminBookEditionSerializer(serializers.ModelSerializer):
# Create new editor
# Create new editor
BookEditor . objects . create ( book_edition = instance , * * ed_data )
BookEditor . objects . create ( book_edition = instance , * * ed_data )
if books_data is not None :
edition . books . set ( books_data )
if edition . book_reference and not edition . books . filter ( id = edition . book_reference_id ) . exists ( ) :
edition . books . add ( edition . book_reference )
elif not edition . book_reference and edition . books . exists ( ) :
first_b = edition . books . first ( )
if first_b :
edition . book_reference = first_b
edition . save ( update_fields = [ ' book_reference ' ] )
author_links_dict = { }
author_links_dict = { }
if has_res_id :
if has_res_id :
author_links_dict [ " researcher_author_id " ] = res_author_id
author_links_dict [ " researcher_author_id " ] = res_author_id