【问题标题】:Django Rest Framework Serializer returns UItextfield instead of the actual valueDjango Rest Framework Serializer 返回 UItextfield 而不是实际值
【发布时间】:2015-12-12 19:07:05
【问题描述】:

所以我正在为我的 API 使用 Django Rest 框架,并尝试为身份验证用户模型创建一个 API。 这是我的序列化程序类

class UserSerializer(serializers.ModelSerializer):
      class Meta:
          model = User
          exclude = ('password',)

我的View类如下

class UserApiDetailView(RetrieveAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

我的网址如下

url(r'^user/(?P<pk>[0-9]+)/$', views.UserApiDetailView.as_view()),

我在调用 API 时得到以下输出

HTTP/1.0 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Date: Sat, 12 Dec 2015 18:54:04 GMT
Server: WSGIServer/0.1 Python/2.7.10
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN
{
    "date_joined": "2015-10-30T06:25:17.400955Z", 
    "email": "<UITextField: 0x7fee18f6fa10", 
    "first_name": "", 
    "groups": [], 
    "id": 2, 
    "is_active": true, 
    "is_staff": false, 
    "is_superuser": false, 
    "last_login": null, 
    "last_name": "", 
    "user_permissions": [], 
    "username": "<UITextField: 0x7fee18f69ad0"
}

我得到的不是用户名和电子邮件的实际值,而是 UITextField: 0x7fee18f69ad0。
我错过了什么吗?
此外,当我使用 ListAPIView 时,一切都会完美运行。

【问题讨论】:

  • 我在 Python 中没有找到与 UITextField 相关的任何内容。您的项目中是否有它?

标签: python django django-rest-framework


【解决方案1】:

我有一个理论。这可能是正在发生的事情:

  • iOS 设备正在使用您的 API。
  • iOS 应用发送UITextField 对象,序列化为字符串,而不是它的值。
  • 当您从 API 查看数据时,您会被数据弄糊涂,因为它看起来像是某个 Python 对象的字符串表示。

调试问题:

在 django admin 或数据库中检查特定用户的 usernamepassword 字段。这将帮助您找到数据库中的真正内容。

【讨论】:

  • 我不敢相信我在这个问题上花了这么多时间。哇,我现在真的很傻。非常感谢您的帮助。
猜你喜欢
  • 2017-02-02
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 2018-07-31
相关资源
最近更新 更多