Skip to content

Add support for dynamically overriding key_args on a form field level #178

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ S3DIRECT_DESTINATIONS = {
'example_destination_two': {
'key': lambda filename, args: args + '/' + filename,
'key_args': 'uploads/images',
},
'example_destination_three': {
'key': lambda filename, args: args['customer_id'] + '/' + filename,
# key_args, are dynamically supplied by the form
}
}
```
Expand Down Expand Up @@ -225,6 +229,25 @@ class S3DirectUploadForm(forms.Form):

__*Optional.__ You can modify the HTML of the widget by overiding template __s3direct/templates/s3direct-widget.tpl__

__*Optional.__ You can supply the __key_args__ parameter when creating the widget in order to dynamically define the resulting s3 keys.
For this to work correctly, __key_args__ must be json serializable:

```python
from django import forms
from s3direct.widgets import S3DirectWidget

class S3DirectUploadForm(forms.Form):
images = forms.URLField(widget=S3DirectWidget(dest='example_destination'))

def __init__(self, *args, **kwargs):
instance = kwargs.get('instance', None)

if instance:
self.base_fields['images'].widget = S3DirectWidget(dest='example_destination_3', key_args={'customer_id':instance.customer.pk})

super().__init__(*args, **kwargs)
```

### views.py

```python
Expand Down
Loading