这适用于游戏mod文件共享网站。我有Apps Mods和游戏。每次抓住,你猜对了,Mods和游戏!
这是我的Mods模型:
from django.db import models
# Register storage backend TODO
def mod_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT/<game>/<filename>
# Example code from https://docs.djangoproject.com/en/1.9/ref/models/fields/
return 'user_{0}/{1}'.format(instance.user.id, filename)
# Create your models here.
class Mods(models.Model):
title = models.CharField(max_length=30)
author = models.CharField(max_length=30)
game = models.ForeignKey(
'games.Games',
on_delete=models.CASCADE,
)
website = models.URLField()
repoWebsite = models.URLField()
upload = models.FileField(upload_to=mod_directory_path)
这是我的游戏模型:
from django.db import models
# Create your models here.
class Games(models.Model):
title = models.CharField(max_length=30)
developer = models.CharField(max_length=30)
website = models.URLField()
slug = models.SlugField()
我想将mod_directory_path自动设置为游戏模型的slu ..
例如,如果Mod项目“Dynamic War Sandbox”具有指向Arma 3游戏的唯一ID的ForeignKey,我希望文件上载路径基于Arma 3的数据库条目的slug。
MEDIA_ROOT/arma-3/<filename>.
我该怎么做?
答案 0 :(得分:0)
这样的事情应该有效。唯一的要求是在创建mod之前游戏中已存在游戏。
def mod_directory_path(instance, filename):
slug = instance.game.slug
return os.sep.join([slug, filename])