<s> import itertools <EOL> from django . db import DEFAULT_DB_ALIAS <EOL> try : <EOL> from django . contrib . admin . utils import NestedObjects <EOL> except ImportError : <EOL> from django . contrib . admin . util import NestedObjects <EOL> def get_related_objects ( obj , using = DEFAULT_DB_ALIAS ) : <EOL> collector = NestedObjects ( using = using ) <EOL> collector . collect ( [ obj ] ) <EOL> def flatten ( elem ) : <EOL> if isinstance ( elem , list ) : <EOL> return itertools . chain . from_iterable ( map ( flatten , elem ) ) <EOL> elif obj != elem : <EOL> return ( elem , ) <EOL> return ( ) <EOL> return flatten ( collector . nested ( ) ) </s>
