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
TypeError: expected string or bytes-like object, when removing an image in Django's UpdateView #98
Comments
The error indicates that the value passed to We may be missing a check for "self":
Can you please debug and check the type and value of the |
I'm hitting this issue. The value sent is Boolean False and match expects a string, so it fails. The exception is thrown in this line: return _compile(pattern, flags).match(string) These are the variable values: flags | 0 This happens when you click on "Clear" checkbox inside a Django's ModelForm. It's a default widget for deleting a FileField value. Here's a full traceback: Traceback: File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view File "/Users/mariogudelj/projects/cp/console/views.py" in console_views_wrapper File "/Users/mariogudelj/projects/cp/console/views.py" in order_edit File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/django/forms/forms.py" in is_valid File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/django/forms/forms.py" in errors File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/django/forms/forms.py" in full_clean File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/django/forms/models.py" in _post_clean File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/django/db/models/base.py" in full_clean File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/django/db/models/base.py" in clean_fields File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/django/db/models/fields/init.py" in clean File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/cloudinary/models.py" in to_python File "/Users/mariogudelj/Envs/cp/lib/python2.7/site-packages/cloudinary/models.py" in parse_cloudinary_resource File "/Users/mariogudelj/Envs/cp/lib/python2.7/re.py" in match Exception Type: TypeError at /console/order-edit/2525308/ |
Can I bump this issue? I'm not sure how to fix it, but I think that to_python method inside CloudinaryField needs to be patched to return value if the type is boolean. Cheers, m |
Any solution? |
I've fixed it changing to_python method. def to_python(self, value):
if isinstance(value, CloudinaryResource):
return value
elif isinstance(value, UploadedFile):
return value
elif value is None or value is False:
return value
else:
return self.parse_cloudinary_resource(value) The problem is the image is kept on cloudinary, but I think is simple to just add the api in here. |
@macarena that the image is kept in Cloudinary shouldn't be a problem, yet you can always remove it manually or as part of the application process. @sunnyville @twoblokeswithapostie Comments are welcome! |
@macarena nailed it. That is the solution indeed. |
Hey @macarena, Thanks, |
As a workaround, I've implemented the following on my form. class UserPhotoForm(forms.ModelForm):
class Meta:
model = User
fields = ("photo",)
def clean_photo(self):
data = self.cleaned_data.get("photo")
if data is False:
data = None
return data |
I remove the migrations folder and then python manage.py makemigrations app_name. It worked for me |
Hello,
I'm using Cloudinary in my Django application, and everything is working fine. Except in my UpdateVIew, when a user checks 'clear' to remove an uploaded image and submit the form this error is shown:
The error page in the browser also shows these highlighted messages:
These are what my model, view and form look like:
models.py:
views.py
forms.py
I think Cloudinary is still expecting something when the field's value is empty. I've used
required=False
because I want the image to be optional as that is how it is in my model. I have looked at the docs and searched the web and I just can't figure out how to fix this.Thanks.
The text was updated successfully, but these errors were encountered: