#31725: Setting ImageField and FileField default to a static file
-------------------------------------+-------------------------------------
               Reporter:  Daniel     |          Owner:  nobody
  González Fernández                 |
                   Type:  New        |         Status:  new
  feature                            |
              Component:  Database   |        Version:  3.0
  layer (models, ORM)                |       Keywords:  static image
               Severity:  Normal     |  default
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I think that the user who made this comment
 (https://code.djangoproject.com/ticket/25905#comment:8) makes a valid
 point.

 This is an important issue because currently, it doesn't allow to serve as
 default an static file (this is useful for example when setting a default
 avatar image in a user models, and in many others use's cases, as it allow
 to have many objects referencing the same file, instead of each one of
 them having a copy)

 In development, where is a common practice to have the media and static
 folders in the root of the project, this is easily solved by doing:
 {{{
 avatar = ImageField(default="../static/default_avatar.png", blank=True).
 }}}
 But what about production, when you usually serve your static files from a
 CND or solucions like AWS S3 ???

 The best solution I've come up with so far is to allow that field to be
 null, and explicitly right the get method of that field:
 {{{
 avatar = ImageField(blank=True, null=true)

 def get_avatar(self):
     # variable PATH_TO_DEFAULT_STATIC_IMAGE depends on the enviroment
     # on development, it would be something like
 "localhost:8000/static/default_avatar.png"
     # on production, it would be something like
 "https://BUCKET_NAME.s3.amazonaws.com/static/default_avatar.png";
     return self.avatar if self.avatar else <PATH_TO_DEFAULT_STATIC_IMAGE>
 }}}

 However, it does look a little bit over-engineer, considering that most of
 the time, the default is an static file.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31725>
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/053.75632c2f33d720dfc8132002158a09d9%40djangoproject.com.

Reply via email to