首页 > 解决方案 > 错误绑定参数 1 - 可能不支持类型 django

问题描述

我正在尝试从 csv 文件中获取图像的 https 路径,下载并将其存储在 CloudinaryField 我尝试 r.content (它包含图像的字节)但 CloudinaryField 不知道,尝试 r.raw 也没有. 如果有最好的方法让我知道谢谢。

这是模型

class ArticleUnite(models.Model):
    libelle = models.CharField(max_length=255)
    image= CloudinaryField('image', overwrite=True,
                    resource_type="image",
                    transformation={"quality": "auto:eco"},
                    format="jpg", blank=True, null=True)
    image2 = CloudinaryField('image', overwrite=True,
                            resource_type="image",
                            transformation={"quality": "auto:eco"},
                            format="jpg", blank=True, null=True)
    reference = models.CharField(max_length=255,blank=True, null=True)
    description = models.CharField(max_length=255,blank=True, null=True)
    auteur = models.CharField(max_length=255, blank=True, null=True,default="sans auteur")
    prix_unitaire_article = models.IntegerField(default=1500)
    is_livre = models.BooleanField(default=False)
    is_cahier = models.BooleanField(default=False)
    is_accessoire = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    def __str__(self):
        return "{}".format(self.libelle)

这是我下载文件然后尝试将其存储在数据库中的代码,但这给了我一个错误绑定

    #print(df['prix'])
    for x in df.index:
        #print(type(df.loc[x,'prix']))
        c = float((df.loc[x,'prix']))
        #cent = 700
        #ct = float(cent)
        #print(ct)
        #print(c)
        if c < 999.0:
            print("<999")
            print(c*1000)
            price = int(c*1000)
            print(price)
            print(df.loc[x,'titre'])
            if df.loc[x,'is_livre']:
                cat_livr = CatalogueLivre.objects.get(libelle="livres")
                print(cat_livr)
                r = requests.get(df.loc[x, 'img'], stream=True)
                if r.status_code == 200:
                    r.raw.decode_content = True
                    print(type(r))
                    with tempfile.NamedTemporaryFile(mode="wb") as f:
                        for chunk in r.iter_content():
                            f.write(chunk)
                        print(f.name)
                        path_img = f.name
                        print(path_img)
                        f.close()
                    article_unite = ArticleUnite.objects.create(
                        libelle=df.loc[x, 'titre'],
                        auteur="autheur",
                        image= r.raw,#trying to store the downloaded file as https://img.jpg
                        image2=r.raw,#trying to store the downloaded file as https://img.jpg 
                        reference=getRandomChaine(8),
                        description=df.loc[x, 'titre'],
                        prix_unitaire_article=price,
                        is_cahier=False,
                        is_livre=True,
                        is_accessoire=False,
                        is_active=True
                    )
                    print(article_unite)
                    livre = Livre.objects.create(
                        articleunite=article_unite,
                        catalogueLivre=cat_livr,
                        editeur=None,
                        annee_edition=str(datetime.year),
                        is_active=True
                    )
                    print(livre)

标签: python-3.xdjangodjango-models

解决方案


推荐阅读