#32852: Attribute error for missing content_type on File object for modeladmin
change request
-------------------------------------+-------------------------------------
Reporter: Aiven Timptner | Owner: nobody
Type: Bug | Status: new
Component: File | Version: 3.2
uploads/storage |
Severity: Normal | Resolution:
Keywords: content_type | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by João Freires):
Replying to [ticket:32852 Aiven Timptner]:
> I've got a model with a FileField and the corresponding admin page
created with modeladmin. To validate the file type and only allow PDFs I
wrote a custom validator which checks the content_type attribute on user
(staff) uploads. When objects are created or the file itself get changed
everything works as expected. But if I change some others fields on the
model I receive an AttributeError.
Yes, all of your validators will be called when you save a model. And your
a assuming that your file has a `content_type` attribute, but only the
file sent to a request has a content type(the TemporaryUploadedFile), when
you change your model and keep your current file, all of your validators
will receive a File and not TemporaryUploadedFile, and the File object
does not have a content type.
You can assume that all pdf files follow the pattern `*.pdf` and check if
the filename ends with the pattern and/or check the types of File,
something like:
{{{#!python
def validate_file_extension(value: File):
"""Check if uploaded file content is pdf"""
if not value.file.name.endswith(".pdf"):
raise ValidationError("Only PDF are allowed")
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32852#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/071.279eb939e50c873a3ec1d4615864cc10%40djangoproject.com.