[英]How i can register on Django admin a field for upload of files
我正在尝试使用lib boto3在django admin中上传文件,但是我认为我没有正确执行此操作。
我正在尝试创建一个表单类,并在admin内部使用它来上传
这是我的forms.py
from django import forms
from django.conf import settings
from .models import Invoice
import boto3
from botocore.exceptions import ClientError
class InvoiceForm(forms.ModelForm):
file = forms.FileField(label='Arquivo', required=False)
class Meta:
fields = '__all__'
model = Invoice
def upload():
s3 = boto3.client('s3')
try:
s3.upload_fileobj(file, settings.UPLOAD_BUCKET)
except ClientError as e:
logging.error(e)
return False
return True
这是我的管理员
from .forms import InvoiceForm
class InvoicesAdmin(admin.ModelAdmin):
search_fields = ['name', 'user']
list_display = ['name', 'user','value', 'installment', 'due_date', 'status', 'file']
exclude = ['updated_by', 'created_by', 'attributes']
autocomplete_fields = ['user']
form = InvoiceForm
admin.site.register(models.Invoice, InvoicesAdmin)
我没有收到错误,但文件未保存到aws-s3
未调用InvoiceForm上的上载方法。 您需要从Django管理员调用的表单上的方法中调用它,例如,保存表单时。
在这里看看表单如何在Django中工作。 https://docs.djangoproject.com/en/2.2/topics/forms/
请记住,如果您要覆盖调用super的方法。
目前的上载方法在调用时将引发错误。 调用上载方法后,您可以从那里调试它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.