获取对象但无法访问属性

时间:2018-04-30 15:28:57

标签: python django

检索设置如下的用户对象时:

class User(models.Model):
    """
    User model
    """
    id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
    email = models.EmailField(max_length=255, null=True, default=None)
    password = models.CharField(max_length=255, null=True, default=None)
    ACCOUNT_CHOICE_UNSET = 0
    ACCOUNT_CHOICE_BRAND = 1
    ACCOUNT_CHOICE_CREATOR = 2
    ACCOUNT_CHOICE_AGENCY = 3
    ACCOUNT_CHOICES = (
        (ACCOUNT_CHOICE_UNSET, 'Unset'),
        (ACCOUNT_CHOICE_BRAND, 'Brand'),
        (ACCOUNT_CHOICE_CREATOR, 'Creator'),
        (ACCOUNT_CHOICE_AGENCY, 'Agency'),
    )
    account_type = models.IntegerField(choices=ACCOUNT_CHOICES, default=ACCOUNT_CHOICE_UNSET)

    class Meta:
        verbose_name_plural = "Users"

    def __str__(self):
        return "%s" % self.email

以下列方式:

        try:
            user = User.objects.get(email=request.data['email'])
        except User.DoesNotExist:
            response_details = {
                    'message': "Invalid email address.",
                    'code': "400",
                    'status': HTTP_400_BAD_REQUEST
            }
            return Response(response_details, status=response_details['status'])

为什么我只能在尝试打印时查看email属性?我也需要能够访问account_type属性。

    if user:
        print(user)

1 个答案:

答案 0 :(得分:5)

您可以为此修改模型的def __str__(self): return "%s %s" % (self.email, self.account_type) 方法:

admin.py

要仅在管理页面上留下电子邮件,您需要自定义管理员类。将其添加到from django.contrib import admin from .models import User class UserAdmin(admin.ModelAdmin): list_display = ('email') admin.site.register(User, UserAdmin) 文件:

final Map<String, String> loginMap = new HashMap<String, String>();
loginMap.put("accounts.google.com", "JWT-Token");
cognitoCredentialsProvider.clear();
cognitoCredentialsProvider.withLogins(loginMap);
cognitoCredentialsProvider.refresh();