I'm trying to use the reverse relation of a OneToOneField
to build the path in an upload_to
method of a FileField
, like this:
def get_upload_path(asset, filename):
# Using the reverse relation `game` here
return '/'.join([asset.game.slug, filename])
class Asset(models.Model)
file = models.FileField(upload_to=get_upload_path)
class Game(models.Model):
slug = models.SlugField()
menu_image = models.OneToOneField(Asset, related_name='game', null=True, blank=True)
I can create a Game
with an empty asset no problem. When I change the game in the admin, and I add a new Asset
to the menu image via the green + button, I get a RelatedObjectDoesNotExist
error saying "Asset has no game" inside the popup. Is there a way to accomplish this? I've found some other answers saying it is, like here and here, but it's not working for me on Django 1.9.4.