简体   繁体   English

int()参数必须是字符串或数字,而不是'SimpleLazyObject'

[英]int() argument must be a string or a number, not 'SimpleLazyObject'

I'm developing a web application via Aliexpress API. 我正在通过Aliexpress API开发Web应用程序。

but I meet some problems such as my title. 但是我遇到了一些问题,例如我的头衔。

I write my view such as below: 我写我的观点如下:

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)

Models: 楷模:

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)

When I try to test to get access from Aliexpress, it'll remind me that int() argument must be a string or a number, not 'SimpleLazyObject' . 当我尝试测试从Aliexpress获取访问权限时,它会提醒我int()参数必须是字符串或数字,而不是'SimpleLazyObject'。 what Can i do. 我能做什么。 Thanks very much Error Traceback like this, it contains more information for me. 非常感谢这样的Error Traceback,它为我提供了更多信息。 But I can't analyze what happened. 但是我无法分析发生了什么。

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'

用户= request.user._wrapped,如果hasattr(request.user,'_ wrapped')否则为request.user

The main cause of your error is here: 您的错误的主要原因是在这里:

#Checking the shop
check = aliexpressToken.objects.filter(user=request.user, aliid=value['aliId'])

Django creates a wrapper class SimpleLazyObject and this will return the actual object once you access any property or member of the object. Django创建一个包装类SimpleLazyObject ,一旦您访问该对象的任何属性或成员,它将返回实际的对象。

So to "wake up" the lazy object: 因此,“唤醒”惰性对象:

#Checking the shop
check = aliexpressToken.objects.filter(user_pk=request.user.pk, aliid=value['aliId'])

This question has other tricks to waking the object, including this one-liner: 这个问题还有其他唤醒对象的技巧,包括以下这种单一方法:

user = request.user._wrapped if hasattr(request.user,'_wrapped') else request.user
check = aliexpressToken.objects.filter(user=user, aliid=value['aliId'])

In addition to the above, all the fields in your model are required; 除上述内容外,还需要模型中的所有字段; so you need to pass all of them, otherwise your model won't get saved. 因此您需要全部通过,否则您的模型将无法保存。

You also don't need to retrieve the object again, you can access the id directly: 您也不需要再次检索该对象,可以直接访问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

This is also very risky: 这也是非常危险的:

#get access_token
value = eval(urllib2.urlopen(req).read())

You should never use eval, and especially never from something that is being received from an external system. 永远不要使用eval,尤其是永远不要使用从外部系统接收到的东西。 If there is ever an error in the feed, you might end up executing code that you don't want. 如果提要中有任何错误,您可能最终会执行不需要的代码。

I assume the library is returning json, you can simply: 我假设库返回的是json,您可以简单地执行以下操作:

import json

value = json.loads(urllib2.urlopen(req).read())

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 int()参数必须是字符串或数字,而不是django中的'SimpleLazyObject' - int() argument must be a string or a number, not 'SimpleLazyObject' in django int() 参数必须是字符串或数字 - int() argument must be a string or a number int()参数必须是字符串或数字,而不是'Choice' - int() argument must be a string or a number, not 'Choice' 为什么int()参数必须是字符串或数字,而不是'list'? - Why int() argument must be a string or a number, not 'list'? TypeError:int()参数必须是字符串或数字,而不是'Binary' - TypeError: int() argument must be a string or a number, not 'Binary' int() 参数必须是字符串或数字,而不是“生成器” - int() argument must be a string or a number, not 'generator' django - int参数必须是字符串或数字,而不是'元组' - django - int argument must be a string or a number, not 'Tuple' Django int()参数必须是字符串或数字 - Django int() argument must be a string or a number TypeError:int()参数必须是字符串或数字,而不是“列表” - TypeError: int() argument must be a string or a number, not 'list' TypeError:int()参数必须是字符串或数字,而不是'tuple' - TypeError: int() argument must be a string or a number, not 'tuple'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM