1

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.

itsafire
  • 5,607
  • 3
  • 37
  • 48
  • This seems to be a question about a design decision in a specific third-party library. Why don't you ask the authors of that library? – Daniel Roseman Dec 05 '17 at 09:52
  • 1
    I am asking this, because it shows some style of programming. The Django ORM is a very mighty tool. So when I see patterns of usage of a technology I can not parse, I just ask. Is this not the basic idea of stackoverflow ? – itsafire Dec 05 '17 at 10:06

1 Answers1

1

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.

SomeTypeFoo
  • 886
  • 7
  • 16