Django如何在models.py中获取用户名?

时间:2022-03-02 22:25:16

I need to get username in my models.py file

我需要在models.py文件中获取用户名

this part of model:

这部分模型:

X = models.FileField(max_length=255, upload_to=get_upload_file_name, blank=True)

calls this function:

调用此函数:

def get_upload_file_name(instance, filename):
    return "uploaded_files/%s_%s" % (str(time()).replace('.','_'), filename)

but I would like to add username to the filename. Could anyone suggest me how do i request username and get as a string in this case?

但我想在文件名中添加用户名。任何人都可以建议我如何请求用户名并在这种情况下获取字符串?

UPDATE:

@login_required
def addnew(request):
    if request.POST:
        form = PForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()   

            return HttpResponseRedirect('/all')

    else:
        form = PForm()

    args = {'full_name': request.user.username}
    args.update(csrf(request))

    args['form'] = form

    return render(request, 'addnew.html', args)

2 个解决方案

#1


The first parameter of get_upload_file_name, instance is the model instance the file belongs to, if that model has a username field, you can simply access this way: instance.username

get_upload_file_name的第一个参数,instance是文件所属的模型实例,如果该模型有username字段,则可以通过以下方式访问:instance.username

#2


Based on your update, following code in your views.py should allow you to pass the username before calling save() on the object:

根据您的更新,您的views.py中的以下代码应该允许您在对象上调用save()之前传递用户名:

...
if form.is_valid():
        obj = form.save(commit=False)
        obj.YOUR_FILE_FIELD = obj.get_upload_file_name(request.user, filename)
        obj.save() 
...

Note that form.save(commit=False) will return you an object, which is not saved to the DB yet (https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method)

请注意,form.save(commit = False)将返回一个对象,该对象尚未保存到DB中(https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-方法)

The get_upload_file_namemethod on the model itself needs to be updated like this:

模型本身的get_upload_file_name方法需要像这样更新:

def get_upload_file_name(username, filename):
    return "uploaded_files/%s_%s_%s" % (username, str(time()).replace('.','_'), filename)

#1


The first parameter of get_upload_file_name, instance is the model instance the file belongs to, if that model has a username field, you can simply access this way: instance.username

get_upload_file_name的第一个参数,instance是文件所属的模型实例,如果该模型有username字段,则可以通过以下方式访问:instance.username

#2


Based on your update, following code in your views.py should allow you to pass the username before calling save() on the object:

根据您的更新,您的views.py中的以下代码应该允许您在对象上调用save()之前传递用户名:

...
if form.is_valid():
        obj = form.save(commit=False)
        obj.YOUR_FILE_FIELD = obj.get_upload_file_name(request.user, filename)
        obj.save() 
...

Note that form.save(commit=False) will return you an object, which is not saved to the DB yet (https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method)

请注意,form.save(commit = False)将返回一个对象,该对象尚未保存到DB中(https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-方法)

The get_upload_file_namemethod on the model itself needs to be updated like this:

模型本身的get_upload_file_name方法需要像这样更新:

def get_upload_file_name(username, filename):
    return "uploaded_files/%s_%s_%s" % (username, str(time()).replace('.','_'), filename)