Open eeintech opened 4 years ago
Hi @Cisco25! The field_class
argument should be a FormField
not a ModelField
. So use django.forms.FilePathField
instead of django.db.FilePathField
: https://docs.djangoproject.com/en/2.2/ref/forms/fields/#filepathfield
You can specify additional arguments to the field class with field_kwargs
or get_field_kwargs
, as shown here: https://django-dynamic-preferences.readthedocs.io/en/latest/quickstart.html#form-fields
Hi @EliotBerriot,
You are right, my mistake. I reread the documentation and now it's clear, sorry I'm starting with Django and there was no example on how to put everything together for the form fields so I was kind of lost. I would suggest adding a simple example like:
field_class = forms.CharField
field_kwargs = {
'max_length': 255,
'widget' : forms.TextInput(attrs={'size':60})
}
def get_field_kwargs(self):
"""
Return a dict of arguments to use as parameters for the field
class instianciation.
"""
kwargs = self.field_kwargs.copy()
print(f'kwargs = {kwargs}')
kwargs.setdefault('label', self.get('verbose_name'))
kwargs.setdefault('help_text', self.get('help_text'))
kwargs.setdefault('widget', self.get('widget'))
kwargs.setdefault('required', self.get('required'))
kwargs.setdefault('initial', self.initial)
kwargs.setdefault('validators', [])
kwargs['validators'].append(self.validate)
return kwargs
I also got mistaken by the name of this field (FilePathField) which I thought who let the user select a folder on his system and return the path, however, it is actually used for listing files and folders inside an already defined folder, which is not what I wanted... I'm gonna keep looking.
Thanks a bunch, Francois
You are right, my mistake. I reread the documentation and now it's clear, sorry I'm starting with Django and there was no example on how to put everything together for the form fields so I was kind of lost. I would suggest adding a simple example like:
It's ok, that's also why we have an issue tracker :)
If you or anyone else wants to turn your snippet into an example in our documentation I'd be happy to review/merge the corresponding pull request.
Take care @Cisco25
I also got mistaken by the name of this field (FilePathField) which I thought who let the user select a folder on his system and return the path
Regarding this specific use case, I'm not sure this is actually possible: as far as I can tell for security reasons, the web browser would never send the filesystem path to the webserver.
Hello,
I'm trying to call a FilePathField model into the FilePreference (I also tried BasePreference) class without success, here is my code in the dynamic_preferences_registry.py:
This is the error I get:
Is there a workaround? I would like the user to be able to select a directory, not a file.
Thank you, Francois