class Album(models.Model):
pictures = ArrayField(models.FileField(upload_to='files'))
}}}
And the following admin.py
{{{
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
admin.site.register(models.Album, AlbumAdmin)
}}}
Creating a new Album in the Django admin works, but:
- Files are not getting saved
- Retrieving a FieldFile is impossible:
{{{
python manage.py 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/25756>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0
Comment:
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/25756#comment:1>
Comment (by riccardodivirgilio):
Hi, I'm implementing an ArrayField and I can attach a "working" and very
dirty implementation.
So far I found several problems:
1. If you use a FileField this field is using a descriptor to transform
strings into File objects, which is why the array field is always
returning strings and not file objects.
2. 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.
3. the pre save method in file field is the one that is actually saving
the file.
--
Ticket URL: <https://code.djangoproject.com/ticket/25756#comment:2>
* Attachment "multiple.py" added.
Comment (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.
--
Ticket URL: <https://code.djangoproject.com/ticket/25756#comment:3>
Comment (by Jack):
How is this situation in 1.11? Is it still not working?
--
Ticket URL: <https://code.djangoproject.com/ticket/25756#comment:4>
Comment (by Alex):
Hello.
I tried to replicate this bug last night, following the posted
instructions.
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
python manage.py 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/25756#comment:5>
Comment (by johnthagen):
Bug still exists in 2.2.10. This transitively affects users of Django REST
Framework who try to serialize multiple Base64 files into a ListField:
{{{
# models.py
from django.contrib.postgres.fields import ArrayField
class Store(Model):
files = ArrayField(models.FileField(upload_to='files', null=True,
blank=True))
}}}
{{{
# serializers.py
from drf_extra_fields.fields import Base64ImageField
from rest_framework.fields import ListField
class StoreSerializer(ModelSerializer):
datasheets = ListField(child=Base64ImageField())
class Meta:
model = Store
fields = '__all__'
}}}
{{{
# views.py
class StoreViewSet(ModelViewSet):
queryset = Store.objects.all()
serializer_class = StoreSerializer
}}}
Doing this results in incorrect results, returning `null` instead of a
file path and failing to save the file. Would be really nice if it was
possible to fix this.
--
Ticket URL: <https://code.djangoproject.com/ticket/25756#comment:6>
* version: 1.8 => 2.2
Comment:
Replying to [comment:6 johnthagen]:
>
> Doing this results in incorrect results, returning `null` instead of a
file path and failing to save the file. Would be really nice if it was
possible to fix this.
Feel-free to prepare a patch.
--
Ticket URL: <https://code.djangoproject.com/ticket/25756#comment:7>
Comment (by Laszlo Marai):
I've just ran into this (after trying to fix a 3rd party ArrayList
field+widget for the admin site). What I've found, after hours of
debugging is basically identical to Ricardo's comment from 5 years ago. I
understand that this is a larger change and probably not too many people
are affected anyway, but could someone please update the documentation to
save time for anyone who may have the idea of using ArrayListField with a
FileField?
Currently, it says (under the `base_field` section):
> Most field types are permitted, with the exception of those handling
relational data (ForeignKey, OneToOneField and ManyToManyField).
This should be extended with ArrayField. Though, it would probably be
better to list the ones that are known to work with it.
--
Ticket URL: <https://code.djangoproject.com/ticket/25756#comment:8>
* Attachment "audi8_9UbJgS3.jpg" added.
ffdfd
Comment (by Claude Paroz):
[https://github.com/django/django/pull/15867 PR] to document the
limitation.
--
Ticket URL: <https://code.djangoproject.com/ticket/25756#comment:9>
Comment (by GitHub <noreply@…>):
In [changeset:"a46dfa87d0ba690125be98f7f1b77062a1794fdc" a46dfa87]:
{{{
#!CommitTicketReference repository=""
revision="a46dfa87d0ba690125be98f7f1b77062a1794fdc"
Refs #25756 -- Doc'd inability to use file fields with PostgreSQL
ArrayField.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25756#comment:10>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"1fd79033f7835c9755f77cb934927c7fd0079da5" 1fd79033]:
{{{
#!CommitTicketReference repository=""
revision="1fd79033f7835c9755f77cb934927c7fd0079da5"
[4.1.x] Refs #25756 -- Doc'd inability to use file fields with PostgreSQL
ArrayField.
Backport of a46dfa87d0ba690125be98f7f1b77062a1794fdc from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25756#comment:11>