Model Helpers

Fetching Records

mediadrop.model.fetch_row(mapped_class, pk=None, extra_filter=None, **kwargs)

Fetch a single row from the database or else trigger a 404.

Typical usage is to fetch a single row for display or editing:

class PageController(object):
    def index(self, id):
        page = fetch_row(Page, id)

    def works_with_slugs_too(self, slug):
        page = fetch_row(Page, slug=slug)

If the pk is string new then an empty instance of mapped_class is created and returned. This is helpful in admin controllers where you may reuse your edit action for adding too.

  • mapped_class – An ORM-controlled model
  • pk (int, None or "new") – A particular primary key to filter by.
  • extra_filter – Extra filter arguments.
  • **kwargs – Any extra args are treated as column names to filter by. See sqlalchemy.orm.Query.filter_by().

An instance of mapped_class.

Raises webob.exc.HTTPNotFound:

If no result is found



Produce a URL-friendly string from the input.

XHTML entities are converted to unicode, and then replaced with the best-choice ascii equivalents.

Parameters:string (unicode) – A title, name, etc
Returns:Ascii URL-friendly slug
Return type:unicode
mediadrop.model.get_available_slug(mapped_class, string, ignore=None, slug_attr='slug', slug_length=50)

Return a unique slug based on the provided string.

Works by appending an int in sequence starting with 2:

  1. awesome-stuff
  2. awesome-stuff-2
  3. awesome-stuff-3
  • mapped_class – The ORM-controlled model that the slug is for
  • string (unicode) – A title, name, etc
  • ignore (Int ID, mapped_class instance or None) – A record which doesn’t count as a collision

A unique slug

Return type:


You're reading the documentation for MediaDrop 0.11dev (current git master). For the latest stable release please consult the documentation for MediaCore CE 0.10.