首页 文章

如何将图片从Google Places API保存到Django应用中的ImageField?

提问于
浏览
1

我正在构建人口代码,用来自Google Places API的图片填充Django模型“City” .

这是模型:

class City(models.Model):
    city_image = models.ImageField(blank=True, upload_to='city_pictures')

我构建了返回照片的API网址:

#fetch image from this url
        city_image_url = ('https://maps.googleapis.com/maps/api/place/photo'
        '?maxwidth=%s'
        '&photoreference=%s'
        '&key=%s') % (maxwidth, city_image_ref, GoogleKey)

Google表示回复是一张照片,我在Postman上对此进行了测试,所以我希望这样:

with urllib.request.urlopen(city_image_url) as response:
            city_image = response
created_city = City.objects.get_or_create(city_id=city_id)[0]
created_city.city_image = city_image 
#plus other fields

但我收到这个错误:

回溯(最近一次调用最后一次):文件“C:\ Users \ bnbih \ Djangos \ excurj_proj \ population_script.py”,第68行,在populate()文件“C:\ Users \ bnbih \ Djangos \ excurj_proj \ population_script.py”中“,第64行,在populate created_city.save()文件”C:\ Users \ bnbih \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ db \ models \ base.py“,第796行,保存force_update = force_update,update_fields = update_fields)文件“C:\ Users \ bnbih \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ db \ models \ base.py” ,第824行,在save_base中更新= self._save_table(raw,cls,force_insert,force_update,using,update_fields)文件“C:\ Users \ bnbih \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ db \ models \ base.py“,第886行,在_save_table中为f表示non_pks]文件”C:\ Users \ bnbih \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ db \ models \ base.py“,第886行,在for_pks中为f]文件”C:\ Users \ bnbih \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ db \ models \ fields \ files.py“,第290行,在pre_save中,如果是file而不是file._committed:AttributeError:'HTTPResponse'对象没有属性'_committed'

我正在使用Python 3和Django 1.10

2 回答

  • 0

    试试这个:

    from django.core.files import File
    from django.core.files.temp import NamedTemporaryFile
    
    city_image = NamedTemporaryFile(delete=True)
    city_image.write(urllib2.urlopen(url).read())
    city_image.flush()
    
    created_city = City.objects.get_or_create(city_id=city_id)[0]
    created_city.city_image = city_image
    
  • 2

    按照上面的答案和this tutorial

    这有助于我将Google地方信息图像保存到ImageField:

    First ,准备HTTP Response为图像的URL:

    city_image_url = ('https://maps.googleapis.com/maps/api/place/photo'
            '?maxwidth=%s'
            '&photoreference=%s'
            '&key=%s') % (maxwidth, city_image_ref, GoogleKey)
    

    Second ,检索该实际图像并将其保存到变量:

    retrieved_image = requests.get(city_image_url)
    

    Third ,在本地创建文件并将远程图像的内容写入其中:

    with open(city_names[i] + '.jpg', 'wb') as f:
                    f.write(retrieved_image.content)
    

    Fourth ,打开包含图像的文件并将其转换为Django文件:

    reopen = open(city_names[i] + '.jpg', 'rb')
                django_file = File(reopen)
    

    Fifth ,创建一个模型对象并将django文件保存在ImageField中,如下所示:

    created_city = City.objects.get_or_create(city_id=city_id)[0]
    #save it to the ImageField -> args are: 1. the name of the file that is to be saved in MEDIA_ROOT path 2. The Django file itself 3. save instance
    created_city.city_image.save(city_names[i] + '.jpg', django_file, save=True)
    created_city.save()
    

    最后,这些是您需要的进口:

    from django.core.files import File
    import requests
    

相关问题