我使用 Django 框架编写了大部分 Web 应用程序。最近我研究了 Ruby on rails 并发现了一些有趣的东西。像 ModelObject.find_or_create_by_id()
这是我的一些 Django 代码
try:
phone = request.POST.get('phone')
customer = Customer.objects.get(phone=phone)
updating customer...
except:
customer=Customer(
creating customer...
)
customer.save()
这段代码看起来很难看。有没有更好的方法来用 Django 做到这一点?