我有(AbstractBaseUser
Django 1.5的)用户模型,我使用电子邮件作为用户名进行身份验证,并具有以下ModelResource
API
class CartItemResource(ModelResource):
product = fields.ForeignKey(CartItemRelatedResource, 'product', full=True)
class Meta:
queryset = CartItem.objects.all()
resource_name = 'cart_item'
excludes = ['creation_date', 'modification_date']
allowed_methods = ['post', 'get', 'delete']
authorization = CartAuthorization()
authentication = SessionAuthentication()
当对API进行GET请求时,我得到:
“用户”对象没有属性“用户名”
编辑用户模型:
class User(AbstractBaseUser):
objects = UserManager()
name = models.CharField(max_length=100)
email = models.EmailField(
max_length=255,
unique=True,
)
phone = models.IntegerField(max_length=10, null=True)
is_admin = models.BooleanField(default=False, blank=True)
is_driver = models.BooleanField(default=False, blank=True)
lastOrderID = models.CharField(max_length=25, null=True)
USERNAME_FIELD = 'email'
#REQUIRED_FIELDS = ['name','phone']
REQUIRED_FIELDS = ['name']
def __unicode__(self):
return self.user
def set_phone(self, phone):
self.phone = phone
class CartAuthorization(Authorization):
def read_list(self, object_list, bundle):
return object_list.filter(cart__user = self.user(bundle), cart__id = bundle.request.GET.get('cart_id'))
我在相同的资源中还有另一个可以工作的POST:
def add_to_cart(self, request, **kwargs):
self.method_check(request, allowed=['post'])
self.is_authenticated(request)
追溯:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\tastypie\resources.py", line 195, in wrapper
response = callback(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\tastypie\resources.py", line 426, in dispatch_list
return self.dispatch('list', request, **kwargs)
File "C:\Python27\lib\site-packages\tastypie\resources.py", line 454, in dispatch
self.throttle_check(request)
File "C:\Python27\lib\site-packages\tastypie\resources.py", line 551, in throttle_check
identifier = self._meta.authentication.get_identifier(request)
File "C:\Python27\lib\site-packages\tastypie\authentication.py", line 283, in get_identifier
return getattr(request.user, username_field)
File "C:\Python27\lib\site-packages\django\utils\functional.py", line 205, in inner
return func(self._wrapped, *args)
AttributeError: 'User' object has no attribute 'username'
当Django包依赖于类似Django 1.4的模型(其中User对象始终具有一个username
字段)时,您很有可能会出现。问题出在您自己的代码中,还是在第三方代码中,但问题没有足够的细节可以说明。
使用完整的追溯来跟踪这些插件
如果有修补程序包,请更新它们
如果没有可用的修补程序,则需要自己对第三方代码进行分叉和修补
关于Django 1.5+用户模型:
https://docs.djangoproject.com/zh-CN/stable/topics/auth/customizing/#auth-custom-user
您应该最有可能id
识别用户,而不是username
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句