в admin.py есть метод:
def save_model(self, request, obj, form, change):
obj.save()
new_obj = Product.objects.latest('id')
image = request.FILES
Upload.upload_files(image, 'product', new_obj.id)
в самом конце кода я заливаю картинку
upload_files:
class Upload:
def upload_files(files, folder_name, id):
print(files)
id = str(id)
path = MEDIA_ROOT + '/images/' + folder_name + '/' + id
if not os.path.exists(path):
os.makedirs(path)
for image in files:
print(image)
extensions = str(image).split('.')
millis = str(round(time.time() * 1000))
def process(f):
with open(path + '/' + millis + extensions[-1],
'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
process(image)
print(files):
<MultiValueDict: {'image': [<InMemoryUploadedFile: bicycle_wheel_drops_blur_101128_1440x900.jpg (image/jpeg)>, <InMemoryUploadedFile: change_admin.png (image/png)>]}>
print(image):
image
Ошибка в браузере :
AttributeError at /admin/products/product/add/
'str' object has no attribute 'chunks'