Sunday, May 31, 2015

Re: django - class based view store post data

I would remove the code entirely for the post() and get(). As it stands, your create() method is never being called via post() since it is normally called by CreateAPIView. From a brief look at the DRF source, the reason you are getting the error that you are is because you are calling self.get_object() within the post() method. Since you are trying to create an object, and create() has not yet been called, there is no object to retrieve. The get_object() used by the *APIView's expects the object to be identified via a URL kwarg (referenced by self.lookup_field), not via attributes that have been posted.

TL;DR; Remove all of the code that you're using to override the methods, with the exception of the create() method (see below). Unless you are doing something outside of the norm (if you don't know what that means, you probably aren't), there shouldn't be a need to override them.

I just found this site, which is like Classy CBV's, except for the DRF, which is amazing: http://www.cdrf.co/3.1/rest_framework.generics/CreateAPIView.html

My guess would be to override create() in your view and add the owner to request.data, something along these lines, (Full disclosure: I've never done this, so YMMV):

def create(self, request, *args, **kwargs):
    request.data['owner'] = request.user
    return super(ViewName, self).create(request, *args, **kwargs)

You may need to play with request.data to figure out the structure and how to modify it. 

If the serializer throws an error complaining about 'owner' being a read-only field, you'll probably need to change the type of field. Just make sure to override update() in a similar as well to keep the user from changing that field.

I don't have a working setup with DRF to test, so YMMV.

For better help, though, you should post your question on the DRF mailing list, rather than here in the Django user list: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework

-James

On Sun, May 31, 2015 at 10:03 AM, Shekar Tippur <ctippur@gmail.com> wrote:
James,

Thanks for responding. 

I have changed the view to:

class AddToUserProfile(generics.CreateAPIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly)
queryset = UserPrefs.objects.all()
serializer_class = UserPrefSerializer
lookup_field = 'pk'
def get(self, request, *args, **kwargs):
return HttpResponse('Hello world')

def post(self, request, *args, **kwargs):
#return self.get(request, *args, **kwargs)
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
def create(self, serializer):
serializer.save(owner=self.request.user)

I get an error: Expected view AddToUserProfile to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/69f2dbec-06a4-4338-953f-39f1f38576c1%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXvfyaxnE9ixB4zmCT8zSOQ4WvvPseY9soyXbi6cpA%2BXw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment