Sunday, May 31, 2015

Re: django - class based view store post data

A few things to consider:

In your view, it looks like you have 'lookup_fields' instead of 'lookup_field'.

http://www.django-rest-framework.org/api-guide/generic-views/#genericapiview

From a quick read through the DRF docs, it also looks like it only requires a single str value, and you are have it surrounded by (), which may lead to confusion later.

You have overridden post() but it doesn't return anything because all of the code is commented out. That'll also throw misleading errors, if it even runs. I'm assuming that may be from you testing things though.

You may also want to look at one of the other views to inherit from, such as the CreateAPIView, which may be more appropriate and efficient for what you are trying to do, rather than just inheriting from the base view class (unless you are trying to do something crazy). It's equivalent to just inheriting from View when using CBV's, which isn't the norm.

It is also possible that you should be POSTing to a URL that contains the 'pk' of the object that you are trying to modify. If you are trying to create an object, I would suggest looking into the CreateAPIView, since the base view probably expects an object pk to work with.

I've only used DRF briefly, so there may be other issues that I haven't spotted.

-James

On May 30, 2015 11:15 PM, "Shekar Tippur" <ctippur@gmail.com> wrote:
Hello,

I am trying to use class based view to post data.
I have come across a weird issue. It is not saving data in the backend. 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. 

I have searched on google to see explanation on this error but could not understand the explanation. Appreciate if someone can shed some light on this.

Here is my model:

class UserPrefs(models.Model):
Preferences = (
(1,'Likes'),
(1,'Dislikes'),
(1,'Shared'),
(1,'Rejected')
)
user = models.ForeignKey('auth.User', related_name='Hello')
name = models.ForeignKey(Stock)
value = models.CharField(max_length=20,choices=Preferences)

class Meta:
#managed = False
db_table = 'user_pref'

def save(self, *args, **kwargs):
super(Hello, self).save(*args, **kwargs)

class AddToUserProfile(generics.GenericAPIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly)
queryset = UserPrefs.objects.all()
serializer_class = UserPrefSerializer
lookup_fields = ('id')
def get(self, request, *args, **kwargs):
return HttpResponse('Hello there!')

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

Serializer code:

class UserPrefSerializer(serializers.ModelSerializer):
owner = serializers.ReadOnlyField(source='owner.username')
class Meta:
model = UserPrefs
fields = ( 'owner', 'name', 'value')
depth = 1

Url.py entry:
url(r'^Hello/setPrefs', views.AddToUserProfile.as_view()),

--
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/6dbb2532-3bc3-4108-b1c9-9d48337fb02c%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%2BciW0wnu__XUuCJOFV6mC8gZ%3DJg1pNpsxec6SgnRkaS-ygA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment