I have my User model (AbstractBaseUser
of Django 1.5) i use email as the username to authenticate and have the following ModelResource
for my 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()
When a GET request is made to the API, I get:
'User' object has no attribute 'username'
Edit User Model:
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'))
I have another another POST in the same resource which works:
def add_to_cart(self, request, **kwargs):
self.method_check(request, allowed=['post'])
self.is_authenticated(request)
Traceback:
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'
You are most likely when Django packages which rely on Django 1.4-like models where User object always has an username
field. The issue is either in your own code or third party code, but the question does not has enough details to tell.
Use a full traceback to trace these addons
Update them if patched package is available
If there is no patch available you need to fork and patch the third party code yourself
About Django 1.5+ user model:
https://docs.djangoproject.com/en/stable/topics/auth/customizing/#auth-custom-user
You should most likely user id
to identify the user, not username