I have a django app that saves games and screenshots. When I save a game I download the screenshots and save them:
class ImageModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
img = models.ImageField()
class Game(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
screenshots = models.ManyToManyField(ImageModel)
screenshots_urls = models.JSONField()
When the game is saved a directory like /games/<game_uuid>/
and /games/<game_uuid>/screenshots/
are created.
Then the screenshots are downloaded and saved in the DB.
Next to display them in a view I was thinking of adding something like this to settings:
GAMES_FOLDER = Path('/games/')
STATIC_URL = 'static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
SCREENSHOTS_URL = GAMES_FOLDER / "GAME_UUID" / "screenshots" / "IMAGE_UUID.png"
SCREENSHOTS_ROOT = ""
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.SCREENSHOTS_URL, document_root=settings.SCREENSHOTS_ROOT)
Then adding a custom tag for screenshots urls since they're not in the common static folder.
Am I complicating this too much?
Do I even need ImageModel if I'm just saving the images to disk, can't I just make game.screenshots an array of image filenames?
Or is there a reason why I might want images saved as ImageModel?
Edit:
I think what I'm looking for is something like
urlpatterns += static(settings.SCREENSHOTS_URL+'<uuid:game_id>/<uuid:image_id>.png', document_root=settings.SCREENSHOTS_ROOT)
Could I work with that to make django search the image in the correct folder?