0

MySQL データベースから取得したユーザーに応じて、動的に割り当てられたディレクトリにファイルをアップロードしようとしています。ドキュメントによると、このuploads_to関数は 2 つの引数しか受け付けないため、別の変数を挿入して返されたパス文字列を完成させることはできないと思いますか? 以下のコード スニペット:

models.py

def item_uploads_to(instance, filename, userName):
    return '/home/' + userName would go here + '/path/to/uploads/' + filename

class Document2(models.Model):
    docfile2 = models.FileField(upload_to=item_uploads_to)

ビュー.py

 userName = 'python' # this is a test value, working DB query code goes here
 item_uploads_to(instance, filename, userName) 
4

1 に答える 1

1

追加の引数を渡すことができないため、すぐには実行できません。ただし、保存しているインスタンスにユーザーを追加できるため、instance.user.usernameまたはを実行して取得できますinstance.username

作成コードでは、次のようになります。

doc = Document2(random stuff but no docfile2)
doc.username = userName
doc.docfile2 = theFile
doc.save()
于 2013-06-24T10:59:13.793 に答える