简体   繁体   中英

Loop value is not JSON serializable in Django

In Django, I have this model:

class CO(models.Model):
    email = models.CharField(max_length=100)

in my view, I try to send email to multiple recipients using:

addresses = CO.objects.all()

data = {
    "personalizations": [
        {
            "to": [{"email": address} for address in addresses],
        }
    ],
...

but I get the following error: <CO: address@gmail.com> is not JSON serializable

I tried with list(PR.objects.values()) and for address in json.dumps(addresses) but I receive bad request.

您可以从地址对象获取电子邮件。

[{"email": address.email} for address in addresses]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM