Fields and forms

There are a number of field classes for conveniently using FileNode objects in your own Django applications.

The following example model contains a ForeignKey field linking to a FileNode object that is associated to a document file. Notice the parameters specifying which media types will be validated, and which should be visible in the widget:

from media_tree.fields import FileNodeForeignKey
from media_tree import media_types
from media_tree.models import FileNode
from django.db import models

class MyModel(models.Model):
    document = FileNodeForeignKey(allowed_media_types=(media_types.DOCUMENT,),
        null=True, limit_choices_to={'media_type__in':
        (FileNode.FOLDER, media_types.DOCUMENT)})

The following example model will allow the user to select a FileNode object associated to an image file:

from media_tree.fields import ImageFileNodeForeignKey
from django.db import models

class MyModel(models.Model):
    image_node = ImageFileNodeForeignKey(null=True)

The following example form will allow the user to select files that are under a specific parent folder named “Projects”:

from media_tree.models import FileNode
from media_tree.fields import FileNodeChoiceField
from django import forms

class MyForm(forms.Form):
    file_node = FileNodeChoiceField(queryset=FileNode.objects.get(
        name="Projects", node_type=FileNode.FOLDER).get_descendants())

For your own applications, the following field classes are available:

class media_tree.fields.DimensionField(verbose_name=None, name=None, **kwargs)

CharField for specifying image dimensions, i.e. width or height. Currently, this needs to be an integer > 0, but since it is a CharField, it might also contain units such as “px” or “%” in the future.

class media_tree.fields.FileNodeChoiceField(allowed_node_types=None, allowed_media_types=None, allowed_extensions=None, level_indicator=u'xa0xa0xa0', rel=None, *args, **kwargs)

A form field for selecting a FileNode object.

Its constructor takes the following arguments that are relevant when selecting FileNode objects:

Parameters:
  • allowed_node_types – A list of node types that are allowed and will validate, e.g. (FileNode.FILE,) if the user should only be able to select files, but not folders
  • allowed_media_types – A list of media types that are allowed and will validate, e.g. (media_types.DOCUMENT,)
  • allowed_extensions – A list of file extensions that are allowed and will validate, e.g. ("jpg", "jpeg")

Since this class is a subclass of ModelChoiceField, you can also pass it that class’ parameters, such as queryset if you would like to restrict the objects that will be available for selection.

label_from_instance(obj)

Creates labels which represent the tree level of each node when generating option labels.

class media_tree.fields.FileNodeForeignKey(allowed_node_types=None, allowed_media_types=None, allowed_extensions=None, level_indicator=u'xa0xa0xa0', *args, **kwargs)

A model field for selecting a FileNode object.

Its constructor takes the following arguments that are relevant when selecting FileNode objects:

Parameters:
  • allowed_node_types – A list of node types that are allowed and will validate, e.g. (FileNode.FILE,) if the user should only be able to select files, but not folders
  • allowed_media_types – A list of media types that are allowed and will validate, e.g. (media_types.DOCUMENT,)
  • allowed_extensions – A list of file extensions that are allowed and will validate, e.g. ("jpg", "jpeg")

Since this class is a subclass of models.ForeignKey, you can also pass it that class’ parameters, such as limit_choices_to if you would like to restrict the objects that will be available for selection.

class media_tree.fields.ImageFileNodeForeignKey(allowed_node_types=None, allowed_media_types=None, allowed_extensions=None, level_indicator=u'xa0xa0xa0', *args, **kwargs)

A model field for selecting a FileNode object associated to a supported image format.

Using this field will ensure that only folders and image files will be visible in the widget, and will require the user to select an image node.

Previous topic

The FileNode model

Next topic

Using FileNodes in templates

This Page