我刚刚开始学习matplotlib
,我想在我的django应用程序中使用它。因此,我想知道如何保存在模型的图像字段中生成的图形,以便可以在需要时进行检索。
matplotlib.pyplot.savefig
接受类似文件的对象作为第一个参数。您可以传递StringIO
/ BytesIO
(根据您的python版本)。
f = StringIO()
plt.savefig(f)
然后,使用django.core.files.ContentFile
将字符串转换为django.core.files.File
(因为FieldFile.save
仅接受的实例django.core.files.File
)。
content_file = ContentFile(f.getvalue())
model_object = Model(....)
model_object.image_field.save('name_of_image', content_file)
model_object.save()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句