I've got a model like this
我有这样的模特
def upload_location(instance, filename): return 'validate/%s/builds/%s' % (get_current_user(), filename) class MidletPair(models.Model): jad_file = models.FileField(upload_to = upload_location) jar_file = models.FileField(upload_to = upload_location) upload_to=tempfile.gettempdir()
How can I get the current user in upload_location()... ?
如何在upload_location()中获取当前用户...?
Side note: Looking up django stuff is confusing as theres a lot of pre-1.0 stuff around on the net.
旁注:看起来django的东西令人困惑,因为网上有很多前1.0的东西。
3 个解决方案
#1
8
The current user is stored in the request object, and you can't get that in a model method unless you pass it in from elsewhere - which you can't do in the upload_to function.
当前用户存储在请求对象中,除非您从其他地方传入,否则无法在模型方法中获取它 - 您无法在upload_to函数中执行此操作。
So you'll need to approach this in a different manner - I would suggest doing it at the form level. You can pass the request object into the form's __init__
method and store it in an instance attribute, where you can get to it in a custom upload handler. For documentation on upload handlers, look here.
因此,您需要以不同的方式处理此问题 - 我建议您在表单级别进行此操作。您可以将请求对象传递到表单的__init__方法,并将其存储在实例属性中,您可以在自定义上载处理程序中将其存储到该属性中。有关上传处理程序的文档,请查看此处。
#2
5
If you added a user field to the model, and have that attribute set before you performed an upload, then you could get the user in to your upload_location function via the instance attribute.
如果您在模型中添加了用户字段,并在执行上载之前设置了该属性,那么您可以通过instance属性将用户输入upload_location函数。
#3
4
First, don't search the net for Django help. Search here only: http://docs.djangoproject.com/en/dev/
首先,不要在网上搜索Django的帮助。仅在此处搜索:http://docs.djangoproject.com/en/dev/
Second, the current user is part of Authentication.
其次,当前用户是身份验证的一部分。
http://docs.djangoproject.com/en/dev/topics/auth/#topics-auth
http://docs.djangoproject.com/en/dev/topics/auth/#topics-auth
The user is recorded in the request. request.user
用户被记录在请求中。 request.user
#1
8
The current user is stored in the request object, and you can't get that in a model method unless you pass it in from elsewhere - which you can't do in the upload_to function.
当前用户存储在请求对象中,除非您从其他地方传入,否则无法在模型方法中获取它 - 您无法在upload_to函数中执行此操作。
So you'll need to approach this in a different manner - I would suggest doing it at the form level. You can pass the request object into the form's __init__
method and store it in an instance attribute, where you can get to it in a custom upload handler. For documentation on upload handlers, look here.
因此,您需要以不同的方式处理此问题 - 我建议您在表单级别进行此操作。您可以将请求对象传递到表单的__init__方法,并将其存储在实例属性中,您可以在自定义上载处理程序中将其存储到该属性中。有关上传处理程序的文档,请查看此处。
#2
5
If you added a user field to the model, and have that attribute set before you performed an upload, then you could get the user in to your upload_location function via the instance attribute.
如果您在模型中添加了用户字段,并在执行上载之前设置了该属性,那么您可以通过instance属性将用户输入upload_location函数。
#3
4
First, don't search the net for Django help. Search here only: http://docs.djangoproject.com/en/dev/
首先,不要在网上搜索Django的帮助。仅在此处搜索:http://docs.djangoproject.com/en/dev/
Second, the current user is part of Authentication.
其次,当前用户是身份验证的一部分。
http://docs.djangoproject.com/en/dev/topics/auth/#topics-auth
http://docs.djangoproject.com/en/dev/topics/auth/#topics-auth
The user is recorded in the request. request.user
用户被记录在请求中。 request.user