#NEWLINE# def get_limit_choices_to_from_path( model , path ) : #NEWLINE# #INDENT# ' Return Q object for limiting choices if applicable.\n\n    If final model in path is linked via a ForeignKey or ManyToManyField which\n    has a `limit_choices_to` attribute, return it as a Q object.\n    ' #NEWLINE# fields = get_fields_from_path ( model , path ) #NEWLINE# fields = remove_trailing_data_field ( fields ) #NEWLINE# limit_choices_to = ( fields and hasattr ( fields [ ( - 1 ) ] , 'rel' ) and getattr ( fields [ ( - 1 ) ] . rel , 'limit_choices_to' , None ) ) #NEWLINE# if ( not limit_choices_to ) : #NEWLINE# #INDENT# return models . Q ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( limit_choices_to , models . Q ) : #NEWLINE# #INDENT# return limit_choices_to #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return models . Q ( ** limit_choices_to )