Python 错误,因为对象属性不存在,除非它存在

Python 错误,因为对象属性不存在,除非它存在,python,django,sendgrid-api-v3,Python,Django,Sendgrid Api V3,我正在尝试使用SendGrid python API发送电子邮件。为此,我使用此功能: def send_contact_us_email(sender=None, name=None, subject=None, message=None): status_code = None if sender and name and subject and message: to = "test@test.com" email = Email.objects.create(to=to,

我正在尝试使用SendGrid python API发送电子邮件。为此,我使用此功能:

def send_contact_us_email(sender=None, name=None, subject=None, message=None):
status_code = None
if sender and name and subject and message:
    to = "test@test.com"
    email = Email.objects.create(to=to, sender=sender, subject=subject, content=message)
    status_code = email.send()

return status_code
所以我使用一个电子邮件模型来存储电子邮件,以供以后可能的参考,这样发送电子邮件的代码就可以放在模型中。以下是电子邮件模型:

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import *

sg = SendGridAPIClient(apikey=getattr(settings, 'SENDGRID_API_KEY'))

class Email(models.Model):
    to = models.EmailField(max_length=100)
    sender = models.EmailField(max_length=100)
    subject = models.CharField(max_length=100)
    content = models.TextField()

    def __str__(self):
        return 'to: {to} from: {sender}'.format(to=self.to, sender=self.sender)

    def send(self):
        if self.to and self.sender and self.subject and self.content:
            to_email = Email(self.to)
            from_email = Email(self.sender)
            content = Content("text/html", self.content)
            mail = Mail(from_email, self.subject, to_email, content)
            response = sg.client.mail.send.post(request_body=mail.get())
            return response.status_code
        return 600 # incomplete information, cannot send email.
当我运行这段代码时,我希望它能正常工作;我创建了一个电子邮件模型对象,其中包含发送电子邮件所需的所有数据,然后使用sendgrid助手类构造一个JSON请求体,供sendgrid API处理

但是,我得到以下错误:

AttributeError at /contact-us/
'Email' object has no attribute 'get'
Request Method: POST
Request URL:    http://127.0.0.1:8000/contact-us/
Django Version: 1.11.13
Exception Type: AttributeError
Exception Value:    
'Email' object has no attribute 'get'
Exception Location: /Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages/sendgrid/helpers/mail/personalization.py in add_to, line 39
Python Executable:  /Users/tomfinet/Desktop/royaleaccounts/env/bin/python
Python Version: 2.7.15
Python Path:    
['/Users/tomfinet/Desktop/royaleaccounts/env/source',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python27.zip',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/plat-darwin',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/plat-mac',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/lib-tk',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/lib-old',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages']
Server time:    Fri, 7 Sep 2018 23:04:16 +0000
Traceback Switch to copy-and-paste view
/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages/django/core/handlers/exception.py in inner
            response = get_response(request) ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages/django/core/handlers/base.py in _get_response
                response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages/django/core/handlers/base.py in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/source/royaleaccounts/views.py in contact_view
        status_code = send_contact_us_email(sender, name, subject, message) ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/source/emails/utils.py in send_contact_us_email
        status_code = email.send() ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/source/emails/models.py in send
            mail = Mail(from_email, self.subject, to_email, content) ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages/sendgrid/helpers/mail/mail.py in __init__
            personalization.add_to(to_email) ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages/sendgrid/helpers/mail/personalization.py in add_to
        self._tos.append(email.get()) ...
▶ Local vars
电子邮件对象没有属性get

但是,当我查看sendgrid助手类时,email类确实有一个get属性:

def get(self):
    """
    Get a JSON-ready representation of this Email.

    :returns: This Email, ready for use in a request body.
    :rtype: dict
    """
    email = {}
    if self.name is not None:
        email["name"] = self.name

    if self.email is not None:
        email["email"] = self.email
    return email

因此,我无法解决此错误,我缺少什么?

当我编写Emailself.sender之类的东西时,它使用的是我的模型,而不是sendgrid编写的helper电子邮件类。因此,只需将sendgrid中的电子邮件作为Emaill或其他内容导入即可

from sendgrid.helpers.mail import Email as Emaill