因此,我尝试使用 request.get 从网络中提取图像并将其保存到 django 数据库中。这是一些代码如下以及我尝试过的步骤:
我的模型(只是需要的部分,不是全部)
class Pistol(models.Model):
gun_id = models.CharField(max_length=255)
manufacturer = models.CharField(max_length=255, blank=True)
model = models.CharField(max_length=255, blank=True)
image = models.ImageField(upload_to='img/inventory', blank=True)
Run Code Online (Sandbox Code Playgroud)
我的代码(我是从 shell 中执行此操作,因此看起来可能有点无组织)
import request
r = requests.get('http://media.server.theshootingwarehouse.com/small/2451.jpg')
item = Pistol(gun_id="1", price=400, image="1.jpg")
item.image.save('1.jpg',r.content)
Run Code Online (Sandbox Code Playgroud)
然后我收到以下错误
AttributeError: 'bytes' object has no attribute 'read'
Run Code Online (Sandbox Code Playgroud)
请注意,我正在工作python3。