What's the rationale of using properties instead of fields within the File model of django-filer

82 Views Asked by At

I tried to do filtering for the extension field, which is actually not a field at all as the following example suggests. For the sake of this example, let's imagine we have a running Django with django-filer installed and some file already uploaded.

>>> from filer.models import File
>>> File.objects.all()[0].extension
'some_ext' # just as example

But when trying to filter:

>>> File.objects.filter(extension='pdf')
django.core.exceptions.FieldError: Cannot resolve keyword 'extension'
into field. Choices are: _file_size, clipboarditem, description, 
downloadfilemodel, file, filer_image_file, folder, folder_id, 
has_all_mandatory_data, id, in_clipboards, is_public, modified_at, 
name, news_attachment, original_filename, owner, owner_id, 
polymorphic_ctype, polymorphic_ctype_id, sha1, uploaded_at

This is because extension are not stored with the model but in fact are computed properties.

My question: What's the rationale of not storing this meta data in the File Model.

Update: I do not complain about the implementation. I ask for the possible motivation of the implementation.

1

There are 1 best solutions below

0
On BEST ANSWER

The extension is not a very important meta since we are storing the file name. If you look at the source code:

@property
def extension(self):
    filetype = os.path.splitext(self.file.name)[1].lower()
    if len(filetype) > 0:
        filetype = filetype[1:]
    return filetype

You can see storing extension would mean we are duplicating the data that we already have. Hence the decision to make it a property.