Utilities

Whenever a developer names something “utils” it really means “stuff I don’t know where to put”.

class awl.utils.URLTree

A tree representation of the django url paths. Each path is stored in a dictionary with its regex pattern, full path, name (if available) and children. Root items in the tree can be accessed through the children list attribute.

__init__()
as_list()

Returns a list of strings describing the full paths and patterns along with the name of the urls. Example:

print_tree()

Convenience method for printing the results of URLTree.as_list to STDOUT

awl.utils.get_field_names(obj, ignore_auto=True, ignore_relations=True, exclude=[])

Returns the field names of a Django model object.

Parameters:
  • obj – the Django model class or object instance to get the fields from

  • ignore_auto – ignore any fields of type AutoField. Defaults to True

  • ignore_relations – ignore any fields that involve relations such as the ForeignKey or ManyToManyField

  • exclude – exclude anything in this list from the results

Returns:

generator of found field names

awl.utils.get_obj_attr(obj, attr)

Works like getattr() but supports django’s double underscore object dereference notation.

Example usage:

>>> get_obj_attr(book, 'writer__age')
42
>>> get_obj_attr(book, 'publisher__address')
<Address object at 105a79ac8>
Parameters:
  • obj – Object to start the derference from

  • attr – String name of attribute to return

Returns:

Derferenced object

Raises:

AttributeError in the attribute in question does not exist

awl.utils.refetch(obj)

Queries the database for the same object that is passed in, refetching its contents in case they are stale.

Parameters:

obj – Object to refetch

Returns:

Refreshed version of the object

awl.utils.refetch_for_update(obj)

Queries the database for the same object that is passed in, refetching its contents and runs select_for_update() to lock the corresponding row until the next commit.

Parameters:

obj – Object to refetch

Returns:

Refreshed version of the object

awl.utils.render_page(request, page_name, data={})

Deprecated since version 0.12: Use django.shortcuts.render instead

This function was a wrapper for render_to_response that handled request context. The django.shortcuts.render method does the same thing, so this just wraps that now.

awl.utils.render_page_to_string(request, page_name, data={})

A shortcut for using render_to_string with a RequestContext automatically.