@@ -1419,7 +1419,7 @@ def response_add(self, request, obj, post_url_continue=None):
14191419
14201420 # Find the optgroup for the new item, if available
14211421 source_model_name = request .POST .get (SOURCE_MODEL_VAR )
1422-
1422+ source_admin = None
14231423 if source_model_name :
14241424 app_label , model_name = source_model_name .split ("." , 1 )
14251425 try :
@@ -1428,21 +1428,23 @@ def response_add(self, request, obj, post_url_continue=None):
14281428 msg = _ ('The app "%s" could not be found.' ) % source_model_name
14291429 self .message_user (request , msg , messages .ERROR )
14301430 else :
1431- source_admin = self .admin_site ._registry [source_model ]
1432- form = source_admin .get_form (request )()
1433- if self .opts .verbose_name_plural in form .fields :
1434- field = form .fields [self .opts .verbose_name_plural ]
1435- for option_value , option_label in field .choices :
1436- # Check if this is an optgroup (label is a sequence
1437- # of choices rather than a single string value).
1438- if isinstance (option_label , (list , tuple )):
1439- # It's an optgroup:
1440- # (group_name, [(value, label), ...])
1441- optgroup_label = option_value
1442- for choice_value , choice_display in option_label :
1443- if choice_display == str (obj ):
1444- popup_response ["optgroup" ] = str (optgroup_label )
1445- break
1431+ source_admin = self .admin_site ._registry .get (source_model )
1432+
1433+ if source_admin :
1434+ form = source_admin .get_form (request )()
1435+ if self .opts .verbose_name_plural in form .fields :
1436+ field = form .fields [self .opts .verbose_name_plural ]
1437+ for option_value , option_label in field .choices :
1438+ # Check if this is an optgroup (label is a sequence
1439+ # of choices rather than a single string value).
1440+ if isinstance (option_label , (list , tuple )):
1441+ # It's an optgroup:
1442+ # (group_name, [(value, label), ...])
1443+ optgroup_label = option_value
1444+ for choice_value , choice_display in option_label :
1445+ if choice_display == str (obj ):
1446+ popup_response ["optgroup" ] = str (optgroup_label )
1447+ break
14461448
14471449 popup_response_data = json .dumps (popup_response )
14481450 return TemplateResponse (
0 commit comments