我正在通过Aliexpress API开发一个Web应用程序。
但我遇到了一些问题,例如我的头衔。
我写下我的观点如下:
def getToken(request):
appKey = 'test12345'
appSecret ='test3456'
redirectUrl = 'http://127.0.0.1:8000/'
#post to submit the request
postdata=urllib.urlencode({
'grant_type':'authorization_code',
'need_refresh_token':True,
'client_id':appKey,
'client_secret':appSecret,
'redirect_uri':redirectUrl,
'code':request.GET['code'],
})
req = urllib2.Request(
url = 'https://gw.api.alibaba.com/openapi/http/1/system.oauth2/getToken/%s' % appKey ,
data = postdata)
#get access_token
value = eval(urllib2.urlopen(req).read())
#Checking the shop
check = aliexpressToken.objects.filter(user=request.user, aliid=value['aliId'])
if check:
return HttpResponseRedirect("/shop/")
else:
result = aliexpressToken(user=request.user, aliid=value['aliId'], resource_owner=value['resource_owner'], refresh_token=value['refresh_token'], access_token=value['access_token'],shop_name='Aliexpress Trade')
result.save()
#get the id of shop which just is added.
shop = aliexpressToken.objects.get(aliid=value['aliId'], user=request.user.id).id
return HttpResponseRedirect("/shop/%s/edit" % shop)
型号:
class aliexpressToken(models.Model):
"""docstring for aliexpressToken"""
user = models.ForeignKey(User)
aliid = models.CharField(max_length=30)
resource_owner = models.CharField(max_length=30)
refresh_token = models.CharField(max_length=50)
access_token = models.CharField(max_length=50)
shop_name = models.CharField(max_length=50)
shop_color = models.CharField(max_length=30)
shop_image = models.CharField(max_length=30)
create_time = models.DateTimeField(auto_now_add=True)
当我尝试测试从Aliexpress获取访问权限时,它会提醒我int()参数必须是字符串或数字,而不是'SimpleLazyObject'。我能做什么。非常感谢 像这样的错误回溯,它包含更多信息给我。但我不能分析发生了什么。
Traceback:
File "D:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "D:\Project\blog\aliexpress\views.py" in getToken
108. check = aliexpressToken.objects.filter(user=request.user, aliid=value['aliId'])
File "D:\Python27\lib\site-packages\django\db\models\manager.py" in filter
155. return self.get_query_set().filter(*args, **kwargs)
File "D:\Python27\lib\site-packages\django\db\models\query.py" in filter
655. return self._filter_or_exclude(False, *args, **kwargs)
File "D:\Python27\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
673. clone.query.add_q(Q(*args, **kwargs))
File "D:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_q
1266. can_reuse=used_aliases, force_having=force_having)
File "D:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_filter
1197. connector)
File "D:\Python27\lib\site-packages\django\db\models\sql\where.py" in add
71. value = obj.prepare(lookup_type, value)
File "D:\Python27\lib\site-packages\django\db\models\sql\where.py" in prepare
339. return self.field.get_prep_lookup(lookup_type, value)
File "D:\Python27\lib\site-packages\django\db\models\fields\related.py" in get_prep_lookup
143. return self._pk_trace(value, 'get_prep_lookup', lookup_type)
File "D:\Python27\lib\site-packages\django\db\models\fields\related.py" in _pk_trace
216. v = getattr(field, prep_func)(lookup_type, v, **kwargs)
File "D:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_lookup
322. return self.get_prep_value(value)
File "D:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
555. return int(value)
Exception Type: TypeError at /gettoken/
Exception Value: int() argument must be a string or a number, not 'SimpleLazyObject'
答案 0 :(得分:3)
user = request.user._wrapped if hasattr(request.user,'_ wrapped')else request.user
答案 1 :(得分:1)
错误的主要原因在于:
#Checking the shop
check = aliexpressToken.objects.filter(user=request.user, aliid=value['aliId'])
Django创建了一个包装类SimpleLazyObject
,一旦访问该对象的任何属性或成员,它将返回实际的对象。
所以“唤醒”懒惰的对象:
#Checking the shop
check = aliexpressToken.objects.filter(user_pk=request.user.pk, aliid=value['aliId'])
This question还有其他技巧来唤醒对象,包括这个单行:
user = request.user._wrapped if hasattr(request.user,'_wrapped') else request.user
check = aliexpressToken.objects.filter(user=user, aliid=value['aliId'])
除上述内容外,还需要模型中的所有字段;所以你需要传递所有这些,否则你的模型将无法保存。
您也不需要再次检索该对象,您可以直接访问该ID:
result = aliexpressToken()
result.user = request.user
result.resource_owner = value['resource_owner']
result.aliid = value['aliId']
result.refresh_token = value['refresh_token']
result.access_token = value['access_token']
result.shop_name = 'Aliexpress Trade'
result.shop_color = ''
result.shop_image = ''
result.save()
shop_id = result.pk
这也是非常危险的:
#get access_token
value = eval(urllib2.urlopen(req).read())
你应该从不使用eval,特别是从来没有从外部系统收到的东西。如果Feed中出现错误,您可能最终会执行您不想要的代码。
我假设库正在返回json,你可以简单地说:
import json
value = json.loads(urllib2.urlopen(req).read())
答案 2 :(得分:0)
将user=request.user
更改为user=request.user.id
:
result = aliexpressToken(user=request.user.id, aliid=value['aliId'], resource_owner=value['resource_owner'], refresh_token=value['refresh_token'], access_token=value['access_token'],shop_name='Aliexpress Trade')