Django model not saving when calling save()

99封情书 提交于 2019-12-10 17:56:48

问题


so i am trying to save a django model, and for some reason i am only getting a 500 internal server error. the thing is,

if i comment the social_auth.save() it works and i can manipulate the object, but not save it

why is this happening? i am using django tastypie and i am trying to save a django-social-auth instance.

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:

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

回答1:


if bundle.obj is of type User, then social_auth.user_id = bundle.obj is wrong and should be social_auth.user = bundle.obj

also ensure you are not in this case:

django-social-auth HTTP 500



来源:https://stackoverflow.com/questions/16591947/django-model-not-saving-when-calling-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!