Re: [Django] #13327: FileField/ImageField accessor methods throw unnecessary exceptions when they are blank or null.

17 views
Skip to first unread message

Django

unread,
Jun 9, 2011, 1:08:58 AM6/9/11
to django-...@googlegroups.com
#13327: FileField/ImageField accessor methods throw unnecessary exceptions when
they are blank or null.
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
kevin.howerton@… | Status: new
Type: | Component: Database layer
Cleanup/optimization | (models, ORM)
Milestone: | Severity: Normal
Version: 1.1 | Keywords:
Resolution: | Has patch: 0
Triage Stage: Design | Needs tests: 0
decision needed | Easy pickings: 0
Needs documentation: 0 |
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------
Changes (by thejaswi_puthraya):

* ui_ux: => 0
* component: Uncategorized => Database layer (models, ORM)
* easy: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/13327#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 22, 2013, 8:30:42 PM3/22/13
to django-...@googlegroups.com
#13327: FileField/ImageField accessor methods throw unnecessary exceptions when
they are blank or null.
-------------------------------------+-------------------------------------
Reporter: kevin.howerton@… | Owner: nobody
Type: | Status: new
Cleanup/optimization | Version: 1.1
Component: Database layer | Resolution:
(models, ORM) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: | Patch needs improvement: 0
Has patch: 0 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by akaariai):

* stage: Design decision needed => Accepted


Comment:

I agree a change here seems like a good idea. The backwards compatibility
needs more investigation than my 2 minute review...

--
Ticket URL: <https://code.djangoproject.com/ticket/13327#comment:6>

Django

unread,
Mar 18, 2016, 7:46:59 PM3/18/16
to django-...@googlegroups.com
#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 timgraham):

#26380 is a duplicate.

--
Ticket URL: <https://code.djangoproject.com/ticket/13327#comment:7>

Django

unread,
Mar 22, 2016, 11:10:33 AM3/22/16
to django-...@googlegroups.com
#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 mehdyhaghy):

any workaround to handle this error?

I get this error in DRF model serializer so I can't suppress it when a
FileField is Null in database.
considering the model definition null values should not cause exception
but it does
{{{
class Repository(models.Model):
owner = models.ForeignKey('auth.User', related_name='file_owner')
file_content= models.FileField(upload_to='repository',null=True,
blank=True)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/13327#comment:8>

Django

unread,
Oct 26, 2016, 7:11:37 AM10/26/16
to django-...@googlegroups.com
#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>

Reply all
Reply to author
Forward
0 new messages