1👍
✅
You should write your own view to wrap around tastypie dispatch_detail :
class UserResource(ModelResource):
[...]
def prepend_urls(self):
return [ url(r'^(?P<resource_name>%s)/$' % self._meta.resource_name, self.wrap_view('current_user'), name='api_current_user') ]
def current_user(self, request, **kwargs):
user = getattr(request, 'user', None)
if user and not user.is_anonymous():
return self.dispatch_detail(request, pk=str(request.user.id))
- [Answer]-Dajaxice randomly stops working
- [Answer]-Using server response as foreign key table entry?
- [Answer]-Django new user: NameError even though class is defined in views
0👍
To get the object for the currently logged in user you can pull this from the request object.
user = request.user
or
user = User.Objects.get(username=user.username)
or using the answer from @gyln
user = get_object_or_404(User, username=user.username)
Obviously if you are doing it this way ensure that the user is authenticated at the view.
Using the object.filter
function will always return a list even if there is just one result; whereas the objects.get
method will only return one result.
- [Answer]-Django, how to create a fallback language in a multilingual website?
- [Answer]-Django error in urls.py
- [Answer]-Does Django store information about who has edited and/or created a record, and if so, where?
Source:stackexchange.com