def _make_admin_link_to_similar(primary_field, *fields, name=None):
    """Create a function that links to a changelist of all objects with similar field values."""
    fields = (primary_field,) + fields
    url_template = '<a href="{url}">{name_or_value}</a>'

    def field_link(self, obj):
        value = getattr(obj, primary_field, None)
        name_or_value = name or value
        filters = {field_name: getattr(obj, field_name) for field_name in fields}
        url = _build_admin_filter_url(obj, filters)
        return format_html(url_template, **locals()) if url else value
    field_link.allow_tags = True
    field_link.short_description = primary_field.replace('_', ' ').capitalize()
    field_link.admin_order_field = primary_field
    field_link.__name__ = field_link.__name__.replace('field', primary_field)

    return field_link
def _make_admin_link_to_similar ( primary_field , * fields , name = None ) : fields = ( primary_field , ) + fields url_template = '<a href="{url}">{name_or_value}</a>' def field_link ( self , obj ) : value = getattr ( obj , primary_field , None ) name_or_value = name or value filters = { field_name : getattr ( obj , field_name ) for field_name in fields } url = _build_admin_filter_url ( obj , filters ) return format_html ( url_template , ** locals ( ) ) if url else value field_link . allow_tags = True field_link . short_description = primary_field . replace ( '_' , ' ' ) . capitalize ( ) field_link . admin_order_field = primary_field field_link . __name__ = field_link . __name__ . replace ( 'field' , primary_field ) return field_link
Create a function that links to a changelist of all objects with similar field values.