Opened 3 years ago

Last modified 9 months ago

#25756 new Bug

ArrayField does not work with FileField

Reported by: Anthony Dong Owned by:
Component: contrib.postgres Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When using the following definition :

from django.contrib.postgres.fields import ArrayField

class Album(models.Model):
    pictures = ArrayField(models.FileField(upload_to='files'))

And the following

from django.contrib.postgres.forms import SplitArrayField
class AlbumForm(forms.ModelForm):
    pictures = SplitArrayField(forms.FileField(), size=2)
    class Meta:
        model = models.Album
        fields = '__all__'

class AlbumAdmin(admin.ModelAdmin):
    form = AlbumForm, AlbumAdmin)

Creating a new Album in the Django admin works, but:

  • Files are not getting saved
  • Retrieving a FieldFile is impossible:
    python shell
    from my_app.models import Album
    Album.objects.get().pictures == [u'filename.png', u'filename2.png']

I would have expected a list of FieldFiles.

Attachments (1) (4.5 KB) - added by Riccardo Di Virgilio 3 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 3 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

I'm not sure to what extent we can fix this, but if not, we can document the limitation or try to add a helpful error message indicating that it's unsupported.

comment:2 Changed 3 years ago by Riccardo Di Virgilio

Hi, I need to implement an array field for multiple files, and currently this is not supported.

after hours of debugging I found out several design problems that are preventing to have a clean implementation of this field.

  1. If you use a FileField this field is using a descriptor to transform strings into File objects (instead of to_python), which is why the array field is always returning strings and not file objects.
  1. The save method of FileField is setting the instance file attribute to a file, which in this case is wrong because it should be a list of files.
  1. the pre save method in file field is the one that is actually saving the file, since ArrayField is not calling the super of that, this cannot work.

array field should support file field out of the box (and this involves also creating a multi file input widget that can work also in the admin).

another solution is to have a special ArrayFileField, which can also return an ArrayFile object that is iterable (instead of a list).

I can attach a "working" and dirty version of it.

Last edited 3 years ago by Riccardo Di Virgilio (previous) (diff)

Changed 3 years ago by Riccardo Di Virgilio

Attachment: added

comment:3 Changed 2 years ago by Nikolai Ryzhkov

Hi. I've faced this limitation.

After quick review of FileField and ArrayField I found out that this problem can't be resolved without redesign of ArrayField.
We can't append a FileField's instance to list which is a instance of ArrayField. The FileField's instance requires access to field (It should get upload_to and storage). So we should have wrapper under list which will be know his field first.

comment:4 Changed 11 months ago by Jack

How is this situation in 1.11? Is it still not working?

comment:5 Changed 9 months ago by Alex

Replying to Jack:

How is this situation in 1.11? Is it still not working?

I tried to replicate this bug last night; I followed the original posted instructions in this ticket.

It appears to still be a valid bug, although difficult to say for sure without a thorough test.

Tested relevant software versions:
Django version 2.0
Python version 3.6
Postgresql version 10.3

Steps followed to reproduce as described in this ticket.

Results observed consistent with those described in this ticket:

Files are not getting saved in any observable place
Retrieving a FileField is impossible:

(same as previously reported)

    python shell
    >>> from my_app.models import Album
    >>> Album.objects.get().pictures == [u'filename.png', u'filename2.png']
    >>> true

Album object appears to incorrectly contain filename strings rather than expected <FieldFile: files/filename.png> FieldFile type data.

Last edited 9 months ago by Alex (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top