prefs' is an invalid keyword argument for this function
Request Method: | POST |
---|---|
Request URL: | http://127.0.0.1:8000/setPrefs/ |
Django Version: | 1.8.2 |
Exception Type: | TypeError Here is my model |
class UserPrefs(models.Model):
Preferences = (
('0','Likes'),
('1','Dislikes'),
('2','Shared'),
('3','Rejected'),
)
user = models.ForeignKey(AuthUser, related_name='Screens')
#user = models.ForeignKey(AuthUser)
address = models.ForeignKey(Address)
prefs = models.CharField(max_length=1,choices=Preferences)
class Meta:
#managed = False
db_table = 'user_pref'
def save(self, *args, **kwargs):
super(Screens, self).save(*args, **kwargs)
Appreciate if someone can shed some light on this.
- Shekar
Can you post the entire traceback for the error, and the view code as well?
-James
--
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...@googlegroups.com.
To post to this group, send email to django...@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/206ffba0-c578-4bbd-8749-7d174d42cc93%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Traceback:
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/views/generic/base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
451. response = self.handle_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
448. response = handler(request, *args, **kwargs)
File "/Users//PycharmProjects///views.py" in post
88. serializer.save()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/rest_framework/serializers.py" in save
165. self.instance = self.create(validated_data)
File "/Users//PycharmProjects///modelserializer.py" in create
48. return Screens.objects.create(**validated_data)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/manager.py" in manager_method
127. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/query.py" in create
346. obj = self.model(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in __init__
480. raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
Exception Type: TypeError at //setPrefs/
Exception Value: 'prefs' is an invalid keyword argument for this function
and View:
class AddToUserProfile(generics.CreateAPIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly)
serializer_class = UserPrefSerializer
queryset = UserPrefs.objects.all()
lookup_field = 'user_id'
#def create(self,request,*args, **kwargs):
def post(self, request, *args, **kwargs):
serializer = UserPrefSerializer(data=request.data)
print (repr(serializer))
user=request.user
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
I just looked over your model again. Your save() override has a super(Screens,...) reference, which doesn't match the model class.
That may explain why you are getting the invalid parameters error, since you are probably calling the wrong save function from a different class.
I'd remove that save() override entirely.
-James
'prefs' is an invalid keyword argument for this function
- Shekar
Wait, what does your serializer look like? I found this in the traceback:
ile "/Users//PycharmProjects///modelserializer.py" in create
48. return Screens.objects.create(**validated_data)
Are you sure that you are referencing the right serializer and/or is the serializer referencing the right model? I wouldn't assume that a create() call would be made for Screens.
-James
class UserPrefSerializer(serializers.ModelSerializer):
#user = serializers.ReadOnlyField(source='owner.username')
def create(self, validated_data):
print ("Validated data")
print (validated_data)
#return Screens.objects.create(**validated_data)
return Screens.objects.create(**validated_data)
curl -H "Authorization: Bearer $usertoken" -H "Content-Type: application/json" -X POST -d '{"address":"XYZ","prefs":"Likes"}' http://${endpoint}/addPrefs
How do I pass the address name so that it gets translated to an id in the background.
- Shekar
class AddToUserProfile(generics.CreateAPIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly)
serializer_class = UserPrefSerializer
queryset = UserPrefs.objects.all()
lookup_field = 'user_id'
#def create(self,request,*args, **kwargs):
def post(self, request, *args, **kwargs):
serializer = UserPrefSerializer(data=request.data)
print (repr(serializer))
#user=request.user
if serializer.is_valid():
print ("validated_data")
print (serializer.validated_data) ## Here i get only prefs - ItemsView(OrderedDict([('prefs', 'Likes')]))
print (request.data) ## Here I see both address as well as prefs {'address': 'XYZ', 'prefs': 'Likes'}
serializer.create(serializer.validated_data)
--
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...@googlegroups.com.
To post to this group, send email to django...@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/8b031b31-3b95-43cd-bcd7-cb46b315703d%40googlegroups.com.
No, it looks like you need to set a model in the Meta class as part of your ModelSerializer class.
http://www.django-rest-framework.org/api-guide/serializers/#modelserializer
Is "XYZ" a valid slug to retrieve that Address object? You probably need to provide the PK value for the 'address' rather than some other field, otherwise you'll be doing some crazy overriding to pull the right Address object.
Your submitted data dictionary is probably going to look something like this:
{"address":"14","prefs":"0"}'
Where 14 is the PK of the Address object you want to use.
I'm a little out in the weeds here since I don't have a DRF setup of my own, so please make sure you read through the docs.
Typically, you won't be submitting the "human readable" version of related (foreign key) field via POST, it will almost always be a PK. The same rule applies for any field that has a 'choices' attribute, you would submit the key value, not the display value. In the case of 'prefs', this would be "1", "3", etc. rather than "Likes" or "Dislikes", per your Preferences list.
API's are made for computers to communicate with other computers, not for people.
-James
--
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...@googlegroups.com.
To post to this group, send email to django...@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/6d31cc5b-41d2-49b4-a193-5743330914f9%40googlegroups.com.
address_obj=Stock.objects.get(symbol=request.data['address'])
user_obj=User.objects.get(username=request.user)
serializer.validated_data['stock_id']=address_obj.id
serializer.validated_data['user_id']=user_obj.id
I am not sure if this is the optimal way to do it but this seem to have done the trick.
--
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...@googlegroups.com.
To post to this group, send email to django...@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/452653a5-6bcc-4e48-9bd8-8d9aea866ecc%40googlegroups.com.
James,
--
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...@googlegroups.com.
To post to this group, send email to django...@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/6782c6e4-451e-4285-9c2b-c9e9abdd3090%40googlegroups.com.