What is the correct implementation of "obj_get" in Django Tastypie?

I am new to Django and Tastypie. I would like to return only one of the objects from the request. I tried almost everything and can not find a solution. Here is my code below:

class ProfileResource(ModelResource): person = fields.ForeignKey(UserResource, 'user', full=True) class Meta: queryset = Person.objects.all() resource_name = 'profile' authentication = BasicAuthentication() authorization = DjangoAuthorization() serializer = Serializer(formats=['json']) 

Now I have a problem with how to return one user object from one resource using request.user .

+6
source share
1 answer

If you want to show only one resource, I would probably create a new resource view (called my_profile) that would invoke a normal detailed view with the user in kwargs and delete the other URLs:

 from django.conf.urls import url from tastypie.utils import trailing_slash class ProfileResource(ModelResource): ... def base_urls(self): return [ url(r"^(?P<resource_name>%s)%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('dispatch_my_profile'), name="api_dispatch_my_profile") ] def dispatch_my_profile(self, request, **kwargs): kwargs['user'] = request.user return super(ProfileResource, self).dispatch_detail(request, **kwargs) 
+4
source

Source: https://habr.com/ru/post/928185/


All Articles