我尝试从模型访问cached_property及其返回
“tuple”对象没有属性“client”
这是具有cached_property的模型
class Creds(models.Model):
tenant = models.OneToOneField('tenant.Tenant', unique=True, on_delete=models.CASCADE)
@cached_property
def client(self):
from pos.api_client import APIClient
return APIClient(self)
这是我尝试访问cached_property的地方。并得到错误。
class createCred(APIView):
def get(self, request, *args, **kwargs):
tenant = get_object_or_404(Tenant, pk=request['tenant_id'])
square_credential = SquareCredential.objects.get_or_create(tenant=tenant)
client = square_credential.client
最佳答案
检查文档中的 get_or_create
。
Returns a tuple of (object, created), where object is the retrieved or created object and created is a boolean specifying whether a new object was created.
所以你需要类似的东西:
square_credential, created = SquareCredential.objects.get_or_create(tenant=tenant)
client = square_credential.client
如果您不关心它是否被创建,您可以使用下划线作为“一次性变量”约定:
square_credential, _ = SquareCredential.objects.get_or_create(tenant=tenant)
关于python - 属性错误: 'tuple' object has no attribute 'client' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55266018/