Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload_to may now be a function #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

briceparent
Copy link

I didn't really test this against older Python/Django versions, but I guess it should work as is.
I didn't try either with another type of callable than a function.

I updated the Kitten example, the thumbnails are now put in a random directory (feel free to remove this, of course).

The instance parameter is only available if there is one, as it is done in https://docs.djangoproject.com/fr/1.9/ref/models/fields/#django.db.models.FileField.upload_to.

Hope it helps

@briceparent
Copy link
Author

Also, I don't know how the packaging works, so I didn't do anything related to this.

@briceparent
Copy link
Author

After some testing, I'm sorry to tell that the second argument to the callback function isn't working as it should.
Normally, it should be called at the end of the process when we already know the file's name, but as it is now, it is called before the form is even displayed, so there is no way to know the name of the file that will be sent. I supposed it worked, because in my sample data, the field already contained an image, so at the time of creating the form, I already knew the file's name and everything seemed to be good.
I'm not sure how to solve this without a way bigger rewrite of the package, and as of now, I'm not going to need it (in my use case, I'm not changing the image's name, just its folder).
If there is a need for it, feel free to modify it or to contact me, and if I find some hours to work on it, I'll try to fix this.
Brice

@jimmykobe1171
Copy link

Actually, the AjaxImageField will have problems on image path if you are not using AdminForm. Since AjaxImageField inherits Field instead of FileField(weird in my opinion), many methods in FileField are now missing, which will cause problems later.

Maybe rewrite the AjaxImageField is a better choice.

Best,
Jimmy

@briceparent
Copy link
Author

I totally agree wth you that the field should extend FileField, as it is a file field and it should be able to replace said fields in already existing forms without much trouble.
But this project doesn't seem to be unit tested, which makes rewrites kind of hard and dangerous, so I'm no going to do it myself (or at least not now).
If I had to do it, I'd probably start a fresh project with unit tests, even if some of them are quite hard to implement with FileFields (maybe this could even be another reason to make a new project, to create an easily unit-tested advanced file field, with some kind of mockup for the uploading part..).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants