#13327: FileField/ImageField accessor methods throw unnecessary exceptions when
they are blank or null.
-------------------------------------+-------------------------------------
Reporter: kevin.howerton@… | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Elias Nygren):
**Some workarounds:**
The model property:
{{{
@property
def image_url(self):
if self.image and hasattr(self.image, 'url'):
return self.image.url
}}}
The Template if (bad, breaks DRY):
{{{
{% if person.image %}
<img src="{{ person.image.url }}">
{% endif %}
}}}
The check (bad, breaks DRY):
{{{
person.profile_pic.url if person.profile_pic else ''
}}}
**IMHO all of the above are quite horrible compared to if we could just
fix the root problem:**
django/db/models/fields/files.py
{{{
def _require_file(self):
if not self:
raise ValueError("The '%s' attribute has no file associated
with it." % self.field.name)
@property
def url(self):
self._require_file()
return self.storage.url(self.name)
}}}
with something equivalent to (not a working fix):
{{{
@property
def url(self):
if self: # only pass to storage.url if there is a file
return self.storage.url(self.name)
return None # (or empty string?)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/13327#comment:9>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/082.06164c4735d892902fd77bf0e9fbd5e5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.