调用save()时Django模式不保存 [英] Django model not saving when calling save()
问题描述
所以我试图保存一个django模型,由于某种原因,我只收到500内部服务器错误。事情是,
如果我评论social_auth.save()它的工作,我可以操纵
对象,但不是保存它
为什么会发生这种情况?我正在使用django tastypie,我正在尝试保存一个django-social-auth实例。
def obj_create(self,bundle,请求=无,** kwargs):
尝试:
#this不应该升级密码
bundle = super(UserResource,self).obj_create(bundle)
bundle。 obj.save()
如果bundle.data.get('extra_data')!=无:
打印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')
printsocia l:,social_auth.extra_data
social_auth.save()
除了IntegrityError:
raise BadRequest('Username already exists')
返回包
traceback:
code>追溯(最近的最后一次呼叫):
文件temp_3.py,第23行,< module>
post()
文件temp_3.py,第18行,在post
f = urllib2.urlopen(req)
文件/usr/lib/python2.7/urllib2 .py,第126行,urlopen
return _opener.open(url,data,timeout)
文件/usr/lib/python2.7/urllib2.py,第406行,以$开头
response = meth(req,response)
文件/usr/lib/python2.7/urllib2.py,第519行,http_response
'http',请求,响应,代码, msg,hdrs)
文件/usr/lib/python2.7/urllib2.py,行444,错误
return self._call_chain(* args)
文件/ usr / lib / python2.7 / urllib2.py,第378行,_call_chain
result = func(* args)
文件/usr/lib/python2.7/urllib2.py,第527行,在http_error_default
中提高HTTPError(req.get_full_url(),代码,msg,hdrs,fp)
urllib2.HTTPError:HTTP错误500:内部服务器错误
/ pre>
解决方案如果bundle.obj的类型为
用户
social_auth.user_id = bundle。 obj
是错误的,应该是social_auth.user = bundle.obj
还确保你不在这种情况:
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
解决方案if bundle.obj is of type
User
, thensocial_auth.user_id = bundle.obj
is wrong and should besocial_auth.user = bundle.obj
also ensure you are not in this case:
这篇关于调用save()时Django模式不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!