使用Django

时间:2019-07-10 11:38:01

标签: django django-models django-rest-framework twilio-api

我想向插入sqlite的号码发送Whatsapp号码。我是python和Django的新手。


class Invoice (models.Model):
    product_name = models.CharField(max_length=255)
    product_description = models.CharField(max_length=255)
    quantity = models.PositiveIntegerField()
    unit_price = models.PositiveIntegerField()
    total_price = models.PositiveIntegerField()
    buyer_name = models.CharField(max_length=100)
    buyer_phone_number = models.CharField(
        validators=[phone_regex], max_length=17)
    date = models.DateTimeField(auto_now_add=True)

    def save(self, *args, **kwargs):
        client = Client(account_sid, auth_token)

        from_whatsapp_number = 'whatsapp:+14155238886'

        to_whatsapp_number = 'whatsapp:%d' % buyer_phone_number

        client.messages.create(body='Invoice created',
                               from_=from_whatsapp_number,
                               to=to_whatsapp_number)

        super(Invoice, self).save(*args, **kwargs)

    def __str__(self):
        return self.product_name

似乎无法读取该变量。这是错误消息:“ buyer_phone_number”未定义

1 个答案:

答案 0 :(得分:0)

buyer_phone_number是一个实例属性。在Python中,您必须始终通过实例本身(即self)引用它们。

   to_whatsapp_number = 'whatsapp:%d' % self.buyer_phone_number