I am using django-nonrel, Postgre as a database and Mongo as file store.
my Model look like this and is working properly
class Doc(models.Model):
created_on = models.DateTimeField(auto_now_add=True)
file = models.FileField(storage=gridfs_storage, upload_to='/')
and is working properly
Doc.objects.all()[0].file.size
108776
Now I am trying to aggregate the size to get the total size of a query set.
I have tried
Doc.objects.all().aggregate(Sum('file__size'))
but this throw
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/zoidberg/dev/backus/lib/python2.6/site-packages/django/db/models/query.py", line 321, in aggregate
is_summary=True)
File "/Users/zoidberg/dev/backus/lib/python2.6/site-packages/django/db/models/sql/query.py", line 974, in add_aggregate
field_list, opts, self.get_initial_alias(), False)
File "/Users/zoidberg/dev/backus/lib/python2.6/site-packages/django/db/models/sql/query.py", line 1417, in setup_joins
raise FieldError("Join on field %r not permitted. Did you misspell %r for the lookup type?" % (name, names[pos + 1]))
FieldError: Join on field 'file' not permitted. Did you misspell 'size' for the lookup type?
enter code here
any idea if that is possible using the ORM or I d have to iterate of the files myself ?
It’s impossible to do this with the ORM since it can only generate aggregations against database fields, while
file.sizeis a dynamic attribute provided by the storage backend.That said, you’re probably better of saving this information in your actual database at upload time, so you can avoid the overhead of iterating over all files.
Now aggregations work as expected since you’re dealing with a database field: