Storing files

In OIOIOI we need some mechanism to exchange files with external judging machines. We use Filetracker for it. It’s our custom module and lives in the Git repository in lib/filetracker.

Filetracker in Django

Most of the Django files docs still apply. The only difference is that we use our custom model field and storage manager, as specified in


To store a field in a model, use code like this:

from django.db import models
from oioioi.filetracker.fields import FileField

class TestFileModel(models.Model):
    file_field = FileField(upload_to='some/folder')

Frequently you don’t want all files from a single field occupy a single folder. No one wants all input files for all tasks to be in a single folder. Therefore in practice you usually pass a filename generator as upload_to. You may do it like this:

from djang.utils.text import get_valid_filename
import os.path

def make_test_filename(instance, filename):
    # instance will be an instance of Test
    # filename will be the name of the file uploaded by the user
    #   or passed programatically to
    return 'problems/%d/%s' % (,

class Test(models.Model):
    problem = models.ForeignKey('Problem')
    input_file = FileField(upload_to=make_test_filename)

To store an existing file, write something like this:

from django.core.files import File
my_model.file_field = File(open('/etc/passwd', 'rb'), name='myfile.txt')
# or:'myfile.txt', File(open('/etc/passwd', 'rb')))

To store a string variable:

from django.core.files import ContentFile
my_model.file_field = ContentFile('content of file', name='myfile.txt')
# or:'myfile.txt', ContentFile('content of file'))

To assign a file, which is already in Filetracker:

from oioioi.filetracker.utils import filetracker_to_django_file
my_model.file_field = filetracker_to_django_file('/path/in/filetracker/to/myfile.txt')


This is assignment, so any output of upload_to will be ignored.

oioioi.filetracker.utils.filetracker_to_django_file(filetracker_path, storage=None)[source]

Returns a File representing an existing Filetracker file (usable only for assigning to a FileField)


Returns the filetracker path of a django.core.files.File.

Accessing Filetracker client

To obtain the Filetracker client class instance, use oioioi.filetracker.client.get_client(). Our Django Filetracker storage backend uses this function as well.


Constructs a Filetracker client.

Needs a FILETRACKER_CLIENT_FACTORY entry in, which should contain a dotted name of a function which returns a filetracker.Client instance. A good candidate is media_root_factory().

The constructed client is cached.


A filetracker factory which sets up local client in settings.MEDIA_ROOT folder.

Indeed, in you will find:

FILETRACKER_CLIENT_FACTORY = 'oioioi.filetracker.client.media_root_factory'

Testing with files

Database fixtures allow to store database entities ready for use in tests. Our FileField is tweaked to also serialize its content, and deserialize automatically when fixtures are loaded.