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

DJ01 with ImageField #82

Closed
mrswats opened this issue Jan 18, 2021 · 5 comments
Closed

DJ01 with ImageField #82

mrswats opened this issue Jan 18, 2021 · 5 comments

Comments

@mrswats
Copy link
Contributor

mrswats commented Jan 18, 2021

Writing today some code I found this

image_downloader/models.py:38:13: DJ01 Avoid using null=True on string-based fields such ImageField. [flake8-django]
  image = models.ImageField(upload_to=path_to_upload, null=True, blank=True)
          ^

And I wonder what's going on here? I am using django-storages to store my images on S3 and, in the end, a URL is stored in the database, that's true, but this error makes no sense whatsoever, correct? Am I missing something?

@rocioar
Copy link
Owner

rocioar commented Jan 19, 2021

Good catch, Django won't allow an empty value to be set on an ImageField, the empty value is actually null so this check is not correct.

Would you like to contribute by removing the ImageField from the List of checks, as it was done on this PR with the UUIDField: #81 ?

@mrswats
Copy link
Contributor Author

mrswats commented Jan 19, 2021

Sure! I will take a look at it later!

@mrswats
Copy link
Contributor Author

mrswats commented Jan 19, 2021

Solved by #84.

@mrswats mrswats closed this as completed Jan 19, 2021
@rsalmaso
Copy link

rsalmaso commented Feb 1, 2021

🤔
https://code.djangoproject.com/ticket/10244
FileField/ImageField are not a CharField, but they behave the same.

and in https://docs.djangoproject.com/en/3.1/ref/models/fields/#filefield

FileField instances are created in your database as varchar columns with a default max length of 100 characters. As with other fields, you can change the maximum length using the max_length argument.

I think it should be reverted.

@rocioar
Copy link
Owner

rocioar commented Feb 1, 2021

@rsalmaso this check was not correct since FileField and ImageField get set to null when there is an empty value so null=True is a valid option: https://github.com/django/django/blob/master/django/forms/fields.py#L553

The fact that they both inherit from CharField, in this case, doesn't change anything, because they both override the to_python method.

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

No branches or pull requests

3 participants