Skip to content

Instantly share code, notes, and snippets.

@walison17
Created June 10, 2020 18:40
Show Gist options
  • Save walison17/b732ce6fa9c20a6d838ac4b68db3e8eb to your computer and use it in GitHub Desktop.
Save walison17/b732ce6fa9c20a6d838ac4b68db3e8eb to your computer and use it in GitHub Desktop.
from django.db import models
from django.contrib.staticfiles.storage import staticfiles_storage
DEFAULT_IMAGE = 'img/default.svg'
class Place(models.Model):
name = models.CharField(max_length=50, unique=True)
image = models.ImageField(upload_to="places/", blank=True, null=True)
def __str__(self):
return self.name
def get_image_url(self):
if not self.image:
return staticfiles_storage.url(DEFAULT_IMAGE)
return self.image.url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment