* 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.
* 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>
Comment (by timgraham):
#26380 is a duplicate.
--
Ticket URL: <https://code.djangoproject.com/ticket/13327#comment:7>
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>
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>