I set a models.FilePathField with the path attribute as an instance of an 
object implementing __str__. I works well until I needed to get the 
corresponding Form Field. It caused the following error:

    TypeError: listdir: path should be string, bytes, os.PathLike, integer 
or None, not StringSettingsReference

StringSettingsReference is my custom Class I mentioned implements the 
__str__ method.

listdir is complaining because there is some code trying to do the 
following:

    for f in sorted(os.listdir(self.path)):

This code is from `django/forms/fields.py` (Django 1.11).

What I want to point out is that Django allowed me to create a model where 
a FilePathField was initialized with a path attribute with type different 
from string, bytes, os.PathLike, integer or None; and then, only Form Field 
related code complains about that (I was using my app for some time and 
realized about this just because decided to reuse the admin site (yes, my 
application is not using form so much)).

Django should complain before (not allowing me to create a model like that) 
or what would be more convenience for me is to not complains about that.

So, I was thinking to fill a bug report, but it is write in the Django 
documentation that it is a good idea to ask here before.

Does this qualify as a Bug?


Addendum:

Why am I using an special class anyway?

Because I wanted the path to be relative to a setting variable, but writing 
anything but a string in the model definition is not allowed by the 
migration serializing mechanism. I mean, I wanted to do the following:

    class MyModel(...).
        .
        .
        .
        a_file_path = models.FilePathField(path=settings.SOMEVARIABLE, ...)

but I can't write SOMEVARIABLE. So I had to create a "deconstructible" 
class:

    @deconstructible
    class StringSettingsReference:
        def __init__(self, name):
            self.name = name
    
        def __str__(self):
            return getattr(settings, self.name)

and use it like:

    a_file_path = 
models.FilePathField(path=StringSettingsReference('SOMEVARIABLE'), ...)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b004cd8e-63cb-4fbb-accb-37d211138bba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to