Reference
This document contains the public Python API reference.
Fields
- class privates.fields.PrivateMediaFieldMixin(*args, **kwargs)
Enable a private media storage for file-based model fields.
Use the mixin in combination with django’s
FileField
class or derivatives to set up the (default) private media storage.- Parameters:
storage – the (private) file storage to use. This defaults to django-privates’ default private storage, but you can subclass these and provide your own. See the upstream storage documentation: https://docs.djangoproject.com/en/4.2/topics/files/#file-storage
- class privates.fields.PrivateMediaFileField(*args, **kwargs)
A generic private media file field.
- class privates.fields.PrivateMediaImageField(*args, **kwargs)
A private media image field.
Views
- class privates.views.PrivateMediaView(**kwargs)
Verify the required permission and send the filefield content via sendfile.
The permissions of the user are verified before sending back any data. If the user has the correct permissions, the path of the specified field name is looked up on the object and passed to sendfile, which transforms it into the appropriate response header so the web-server can serve the file contents.
- Parameters:
permission_required – the permission required to view the file
model – the model class to look up the object
- file_field: str = ''
Name of the file field on the model to look up.
The path (on-disk) of the file is passed along to
django_sendfile.sendfile()
.
- sendfile_options: dict | None = None
Additional options for
django_sendfile.sendfile()
.
Admin
- class privates.admin.PrivateMediaMixin
Enable downloading private-media model fields in the admin.
Use this mixin to automatically replace the Django admin file field widget so that private media fields can be downloaded. Django’s default behaviour is to create URLs that are not actually served by the webserver (directly, and by design). Instead, a custom view is installed for each private media field of the model and registered in the admin URL configuration (automatically).
- get_private_media_fields() Sequence[str]
Return a sequence of model field names that are private media fields.
By default, all private media fields are detected automatically. You can override this with the
private_media_fields
attribute or by overriding this method.
- get_private_media_permission_required(field: str) str
Return the permission required to download private media field contents.
The permission is specified in Django’s typical format:
{app_label}.change_{model_name}
. By default, users are allowed to download the model field contents if they have change permissions for the model.Override this method if you want to use permissions specific to a particular field or set of fields, or specify
private_media_permission_required
for an alternative static permission.- Parameters:
field – name of the private media field
- get_private_media_view(field: str)
Construct a view for the given private media field on the model.
- get_private_media_view_options(field: str) dict
Specify any additional options to forward to
django_sendfile.sendfile()
.
- private_media_file_widget
alias of
PrivateFileWidget
- private_media_no_download_fields: Sequence[str] = ()
A list of field names for which downloads are forbidden.
By default, the contents for private media field are made downloadable through the custom widget. You can block this by specifying the name of the field(s) that should only be writable and not downloadable.
- private_media_view_class
The Django view class to use for private media field content download views.
alias of
PrivateMediaView
Storages
- class privates.storages.PrivateMediaFileSystemStorage(*args, **kwargs)
Storage that puts files in the private media folder that isn’t globally available.
settings.PRIVATE_MEDIA_ROOT
is used to determine where to write the filessettings.PRIVATE_MEDIA_URL
is the internal URL used for files.
- class privates.storages.PrivateMediaStorage
Lazily initialized
PrivateMediaFileSystemStorage
- privates.storages.private_media_storage = <privates.storages.PrivateMediaStorage object>
A default (lazy) private media storage, configured via Django settings.