【问题标题】:Django model not saving when calling save()调用 save() 时 Django 模型不保存
【发布时间】:2013-05-11 14:42:18
【问题描述】:

所以我试图保存一个 django 模型,但由于某种原因,我只收到 500 个内部服务器错误。问题是,

如果我评论 social_auth.save() 它可以工作,我可以操纵 对象,但不保存它

为什么会这样?我正在使用 django sweetpie,我正在尝试保存一个 django-social-auth 实例。

def obj_create(self, bundle, request=None, **kwargs):
    try:
        #this is not supposed to upgrade password
        bundle = super(UserResource, self).obj_create(bundle)
        bundle.obj.save()
        if bundle.data.get('extra_data') != None:
            print bundle.data.get('extra_data')
            fb_id = bundle.data.get('extra_data')['id']
            #social_auth=UserSocialAuth(user_id = bundle.obj, provider=bundle.data.get('provider'),uid=fb_id,extra_data=bundle.data.get('extra_data') )
            social_auth=UserSocialAuth()
            social_auth.user_id = bundle.obj
            social_auth.provider=bundle.data.get('provider')
            social_auth.uid=fb_id
            social_auth.extra_data=bundle.data.get('extra_data')


            print "social: ",social_auth.extra_data
            social_auth.save()

    except IntegrityError:
        raise BadRequest('Username already exists')

    return bundle

追溯:

Traceback (most recent call last):
  File "temp_3.py", line 23, in <module>
    post()
  File "temp_3.py", line 18, in post
    f = urllib2.urlopen(req)
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 406, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 444, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: INTERNAL SERVER ERROR

【问题讨论】:

  • 堆栈跟踪说明了什么?
  • 没什么,但我只是在上面添加了回溯
  • 如果bundle.obj的类型是User,那么social_auth.user_id = bundle.obj是错误的,应该是social_auth.user = bundle.obj
  • 就是这样,现在可以了...您可以写一个答案,我会将其标记为正确...谢谢

标签: python django django-models django-socialauth tastypie


【解决方案1】:

如果bundle.obj是User类型,那么social_auth.user_id = bundle.obj是错误的,应该是social_auth.user = bundle.obj

还要确保你不在这种情况下:

django-social-auth HTTP 500

【讨论】:

    猜你喜欢
    • 2013-09-24
    • 2018-11-19
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多