jazzband / jazzband/django-model-utils
tracker.previous on FileField does not work
- Dominant language
- Python
- Stars
- 2.8k
- Forks
- 374
- PR merge metrics
- No merged PRs in 30d
Description
Django and model_utils versions are:
> > > django.VERSION
> > > (1, 5, 5, 'final', 0)
> > > model_utils.**version**
> > > '1.5.0'
Repro steps:
Create model.
``` python
class NewModel(models.Model):
my_file = models.FileField(upload_to='.')
tracker = FieldTracker(fields=['my_file',])
```
In admin upload some file, save. Try to get previous file field.
> > > f = NewModel.objects.get(id=1)
> > >
> > > prev_file = f.tracker.previous('my_file')
> > >
> > > prev_file.path
Traceback (most recent call last):
File "", line 1, in
File "/Users/andriyko/.virtualenvs/rfdocset/lib/python2.7/site-packages/django/db/models/fields/files.py", line 59, in _get_path
return self.storage.path(self.name)
AttributeError: 'FieldFile' object has no attribute 'storage'
Workaround (assumes I have some default_storage):
``` python
#did not want to override .previous() method and do field type checking
class CustomFieldInstanceTracker(FieldInstanceTracker):
def previous_file(self, field, storage=None):
new_field = self.previous(field)
setattr(new_field, 'storage', storage or default_storage)
return new_field
```
Then in models.py patch FieldTracker:
``` python
FieldTracker.tracker_class = CustomFieldInstanceTracker
```
Use as:
> > > prev_file = f.tracker.previous_file('my_file')
> > >
> > > prev_file.path
u'/Users/andriyko/Projects/pr_test/test_files/test.txt'
Contributor guide
Research direction
Start with FieldTracker.previous and the FieldInstanceTracker workaround shown in models.py, then compare the behavior with Django's django/db/models/fields/files.py traceback. Reproduce the FileField case from the issue; done means tracker.previous('my_file') returns a value whose path can be accessed without the custom workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100