我使用以下代码发送短信.
from twilio.rest import Client
from django.conf import settings
def send_sms(phone, content=generate_sms_code()):
client = Client(settings.ACCOUNT_SID, settings.AUTH_TOKEN)
return client.messages.create(
to=phone,
from_=settings.FROM_DEFAULT_NUMBER,
body=content)
Run Code Online (Sandbox Code Playgroud)
嘲笑twilio api的正确方法是什么?
我在用 twilio 6.5.2
我有一个定义枚举的模型,如下所示:
enum status: [ :init, :requested, :packed, :paid, :shipped ]
我还有以下使用脏属性的方法。
def shipment_requested
status_changed?(from: :init, to: :requested)
end
def shipment_packed
status_changed?(from: :requested, to: :packed)
end
def shipment_paid
status_changed?(from: :packed, to: :paid)
end
def shipment_shipped
status_changed?(from: :paid, to: :shipped)
end
Run Code Online (Sandbox Code Playgroud)
有很多模型回调依赖于此,例如更新时间戳和发送电子邮件。
但不幸的是这些都不起作用。
sh = Shipment.find(1)
sh.init?
=> true
sh.requested!
sh.requested?
=> true
sh.shipment_requested
=> false
Run Code Online (Sandbox Code Playgroud)
上面的代码有什么问题?这是一个错误吗?
我可以在控制器上手动设置所有值,但这违背了使用枚举的目的,枚举具有根据上面的文档提供的非常好的功能,例如定义范围和方法来检查status