ApiKey Authentication issue 'SimpleLazyObject' with GET while accessing request.user

48 views
Skip to first unread message

Ashish Tiwari

unread,
Feb 6, 2015, 12:36:24 AM2/6/15
to django-...@googlegroups.com
Hi all,

I'm new to tastypie, I'm facing issue with the ApiKeyAuthentication with GET method.

I created an Account model that have foreign-key of User.

class Account(models.Model):
    user = models.ForeignKey(User)
    uuid = models.CharField(max_length = 36)
    score = models.CharField(max_length = 36)

class Practice(models.Model):
    account = models.ForeignKey(Account)
    practice_type=models.CharField(max_length = 36)
    app_id=models.CharField(max_length = 36)


I want to update the Account's score when user passing "app_id" and "practice_type" , Below is PracticeResource.
I'm using prepend_url method to pass the "app_id" and "practice_type".

class PracticeResource(ModelResource):
    class Meta:
        queryset = Practice.objects.all()
        resource_name = 'practice'
        authorization = Authorization()
        authentication = MultiAuthentication(ApiKeyAuthentication(), SessionAuthentication())
        always_return_data=True

    def prepend_urls(self):
        return [
        url(r"^(?P<resource_name>%s)/levels%s$"%(self._meta.resource_name,trailing_slash()),self.wrap_view('get_practice_levels'), name="api_get_levels"),
        ]

    def get_practice_levels(self,request,**kwargs):
    # some rules goes here, if practice_type= 1 then score 100, if 2 then 200 .....etc

    account = Account.objects.get(user=request.user)
    account.score="100"
    account.save()


I'm passing Authorization information in header,

curl -i -H "Authorization: ApiKey AbdH:65ca30f75933b8aec" -H "Content-Type: application/json" http://localhost:8000/api/v1/practice/levels/?app=e2c2c1e6-d886-4efe-bd0e-43db839deac8&type=1


Tastypie throws an error at 

500 : Internal Server Error

>>> account = Account.objects.get(user=request.user)
TypeError: "int() argument must be a string or a number, not 'SimpleLazyObject'


Thanks.

Ashish T.

unread,
Feb 9, 2015, 2:22:37 AM2/9/15
to django-...@googlegroups.com
Figured out the issue and solved it by doing following code changes.

account = Account.objects.get(user__username=username)


For api_key authentication in function, I used.

        self.method_check(request, allowed=['post','get'])
        self.is_authenticated(request)
        self.throttle_check(request)
Reply all
Reply to author
Forward
0 new messages