Models and Managers

class media_tree.models.FileNode(*args, **kwargs)

Each FileNode instance represents a node in the media object tree, that is to say a “file” or “folder”. Accordingly, their node_type attribute can either be FileNode.FOLDER, meaning that they may have child nodes, or FileNode.FILE, meaning that they are associated to media files in storage and are storing metadata about those files.

Note

Since FileNode is a child class of MPTTModel, it inherits many methods that facilitate queries and data manipulation when working with trees.

You can access the actual media associated to a FileNode model instance using the following fields:

file
The actual media file
preview_file
An optional image file that will be used for previews. This is useful for visual media that PIL cannot read, such as video files.

These fields are of the class FileField. Please see Configuring Media Tree for information on how to configure storage and media backend classes. By default, media files are stored in a subfolder uploads under your media root.

get_admin_url(query_params=None, use_path=False)

Returns the URL for viewing a FileNode in the admin.

get_ancestors(ascending=False, include_self=False)

Creates a QuerySet containing the ancestors of this model instance.

This defaults to being in descending order (root ancestor first, immediate parent last); passing True for the ascending argument will reverse the ordering (immediate parent first, root ancestor last).

If include_self is True, the QuerySet will also include this model instance.

get_caption_formatted(field_formats={'title': '<strong>%s</strong>'}, escape=True)

Returns object metadata that has been selected to be displayed to users, compiled as a string including default formatting, for example bold titles.

You can use this method in templates where you want to output image captions.

get_caption_formatted_unescaped()

Returns object metadata that has been selected to be displayed to users, compiled as a string with the original field values left unescaped, i.e. the original field values may contain tags.

get_children()

Returns a QuerySet containing the immediate children of this model instance, in tree order.

The benefit of using this method over the reverse relation provided by the ORM to the instance’s children is that a database query can be avoided in the case where the instance is a leaf node (it has no children).

If called from a template where the tree has been walked by the cache_tree_children filter, no database query is required.

get_descendant_count()

Returns the number of descendants this model instance has.

get_descendants(include_self=False)

Creates a QuerySet containing descendants of this model instance, in tree order.

If include_self is True, the QuerySet will also include this model instance.

get_leafnodes(include_self=False)

Creates a QuerySet containing leafnodes of this model instance, in tree order.

If include_self is True, the QuerySet will also include this model instance (if it is a leaf node)

get_level()

Returns the level of this node (distance from root)

get_metadata_display(field_formats={}, escape=True)

Returns object metadata that has been selected to be displayed to users, compiled as a string.

get_metadata_display_unescaped()

Returns object metadata that has been selected to be displayed to users, compiled as a string with the original field values left unescaped, i.e. the original field values may contain tags.

get_next_sibling(**filters)

Returns this model instance’s next sibling in the tree, or None if it doesn’t have a next sibling.

get_previous_sibling(**filters)

Returns this model instance’s previous sibling in the tree, or None if it doesn’t have a previous sibling.

get_qualified_file_url(field_name='file')

Returns a fully qualified URL for the file field, including protocol, domain and port. In most cases, you can just use file.url instead, which (depending on your MEDIA_URL) may or may not contain the domain. In some cases however, you always need a fully qualified URL. This includes, for instance, embedding a flash video player from a remote domain and passing it a video URL.

get_qualified_preview_url()

Similar to get_qualified_file_url(), but returns the URL for the preview_file field, which can be used to associate image previews with video files.

get_root()

Returns the root node of this model instance’s tree.

get_siblings(include_self=False)

Creates a QuerySet containing siblings of this model instance. Root nodes are considered to be siblings of other root nodes.

If include_self is True, the QuerySet will also include this model instance.

static get_top_node()

Returns a symbolic node representing the root of all nodes. This node is not actually stored in the database, but used in the admin to link to the change list.

insert_at(target, position='first-child', save=False, allow_existing_pk=False)

Convenience method for calling TreeManager.insert_node with this model instance.

is_ancestor_of(other, include_self=False)

Returns True if this model is an ancestor of the given node, False otherwise. If include_self is True, also returns True if the two nodes are the same node.

is_child_node()

Returns True if this model instance is a child node, False otherwise.

is_leaf_node()

Returns True if this model instance is a leaf node (it has no children), False otherwise.

is_root_node()

Returns True if this model instance is a root node, False otherwise.

is_top_node()

Returns True if the model instance is the top node.

move_to(target, position='first-child')

Convenience method for calling TreeManager.move_node with this model instance.

NOTE: This is a low-level method; it does NOT respect MPTTMeta.order_insertion_by. In most cases you should just move the node yourself by setting node.parent.

FILE = 200

The constant denoting a file node, used for the node_type attribute.

FOLDER = 100

The constant denoting a folder node, used for the node_type attribute.

alt

Returns object metadata suitable for use as the HTML alt attribute. You can use this method in templates:

<img src="{{ node.file.url }}" alt="{{ node.alt }}" />
author = None

Author name of the file

copyright = None

Copyright information for the file

created = None

Date and time when object was created

created_by

User that created the object

date_time = None

Date and time information for the file (authoring or publishing date)

description = None

Description for the file

extension = None

File extension, lowercase

extra_metadata = None

Extra metadata

files = <media_tree.models.FileNodeManager object at 0x3ba0610>

A special manager with the same features as objects, but only displaying file nodes, no folder nodes.

folders = <media_tree.models.FileNodeManager object at 0x3ba05d0>

A special manager with the same features as objects, but only displaying folder nodes.

has_metadata = None

Flag specifying whether the absolute minimal metadata was entered

height = None

For images: height in pixels

is_default = None

Flag whether the file is the default file in its parent folder

keywords = None

Keywords for the file

media_type = None

Media type, i.e. broad category of the kind of media

mimetype = None

The mime type of the media file

modified = None

Date and time when object was last modified

modified_by

User that last modified the object

name = None

Name of the file or folder

node_type = None

Type of the node (FileNode.FILE or FileNode.FOLDER)

objects = <media_tree.models.FileNodeManager object at 0x3ba0550>

An instance of the FileNodeManager class, providing methods for retrieving FileNode objects by their full node path.

override_alt = None

Alt text override. If empty, the alt text will be compiled from the all metadata that is available and flagged to be displayed.

override_caption = None

Caption override. If empty, the caption will be compiled from the all metadata that is available and flagged to be displayed.

parent

The parent (folder) object of the node.

position = None

Position of the file among its siblings, for manual ordering

publish_author = None

Flag to toggle whether the author name should be displayed

Flag to toggle whether copyright information should be displayed

publish_date_time = None

Flag to toggle whether date and time information should be displayed

published = None

Publish date and time

published_objects = <media_tree.models.FileNodeManager object at 0x3ba0590>

A special manager with the same features as objects, but only displaying currently published objects.

size = None

File size in bytes

slug = None

Slug for the object

title = None

Title for the file

tree = <mptt.managers.TreeManager object at 0x3ba0510>

MPTT tree manager

width = None

For images: width in pixels

class media_tree.models.FileNodeManager(filter_args={})

A special manager that enables you to pass a path argument to get(), filter(), and exclude(), allowing you to retrieve FileNode objects by their full node path, which consists of the names of its parents and itself, e.g. "path/to/folder/readme.txt".

exclude(*args, **kwargs)

Works just like the default Manager’s exclude() method, but you can pass an additional keyword argument named path specifying the full path of the folder whose immediate child objects you want to exclude, e.g. "path/to/folder".

filter(*args, **kwargs)

Works just like the default Manager’s filter() method, but you can pass an additional keyword argument named path specifying the full path of the folder whose immediate child objects you want to retrieve, e.g. "path/to/folder".

get(*args, **kwargs)

Works just like the default Manager’s get() method, but you can pass an additional keyword argument named path specifying the full path of the object you want to retrieve, e.g. "path/to/folder/readme.txt".

Project Versions

Previous topic

Admin interface overview

Next topic

Configuring Media Tree

This Page