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:
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.
A form field for selecting a FileNode object.
Its constructor takes the following arguments that are relevant when selecting FileNode objects:
Parameters: |
|
---|
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.
Creates labels which represent the tree level of each node when generating option labels.
A model field for selecting a FileNode object.
Its constructor takes the following arguments that are relevant when selecting FileNode objects:
Parameters: |
|
---|
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.
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.