I try to POST request but I got KeyError.
Exception Location: C:\github\dj-postgres-heroku\get_staff\serializers.py, line 32, in create
Here is error location and that line is this.
def create(self, validated_data):
**profile_data = validated_data.pop('profile')**
I try to POST data like this.
enter image description here
enter image description here
{
"profile":1,
"title": "일단 10만원, 업무 마친 후 바로 입금",
"hourly_pay": 12500,
"start_date": "2020-05-06",
"end_date": "2020-05-09",
"start_time": "09:00:00",
"end_time": "18:00:00",
"content": "간단업무입니다.",
"jobs":[1, 2]
}
Here are my code all.
models.py
class GetStaffPost(models.Model):
profile = models.ForeignKey(Profile, null=True, on_delete=models.CASCADE) # default=1 -> pk=1
title = models.CharField(max_length=30)
jobs = models.ManyToManyField(Job, blank=True) # tag랑 비슷
hourly_pay = models.IntegerField(null=True)
start_date = models.DateField(null=True)
end_date = models.DateField(null=True)
start_time = models.TimeField(null=True)
end_time = models.TimeField(null=True)
created_at = models.DateTimeField(null=True, auto_now_add=True)
content = models.TextField(default='content')
class Meta:
ordering = ['-created_at']
def __str__(self):
return f'{self.pk}: {self.title} - {self.profile.nickname}'
serializers.py
class GetStaffPostSerializer(serializers.ModelSerializer):
profile = serializers.PrimaryKeyRelatedField(read_only=True)
jobs = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
class Meta:
model = GetStaffPost
# fields = ['id', 'profile', 'title', 'jobs', 'hourly_pay', 'start_date', 'end_date',
# 'start_time', 'end_time', 'created_at', 'content']
fields = "__all__"
def create(self, validated_data): # profile, jobs
profile_data = validated_data.pop('profile')
profile_id = profile_data
profile = Profile.objects.get(pk=profile_id)
jobs_data = validated_data.pop('jobs') # list?
post = GetStaffPost.objects.create(**validated_data,
profile=profile) # Direct assignment to the forward side of a many-to-many set is prohibited. Use jobs.set() instead.
for job_data in jobs_data:
post.jobs.add(Job.objects.get(pk=job_data))
return profile
views.py
class GetStaffPostViewSet(viewsets.ModelViewSet):
queryset = GetStaffPost.objects.all()
serializer_class = GetStaffPostSerializer
def perform_create(self, serializer):
serializer.save()
when POST request , I want to write only profile pk and jobs pk list. not all fields in model Profile and Jobs.
And when GET request, I want to show all fields.
This is Traceback.
[17/Nov/2021 12:31:43] "GET /get_staff/post/ HTTP/1.1" 200 16613
Internal Server Error: /get_staff/post/
Traceback (most recent call last):a
File "C:\github\dj-postgres-heroku\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\github\dj-postgres-heroku\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\github\dj-postgres-heroku\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\github\dj-postgres-heroku\venv\lib\site-packages\rest_framework\viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "C:\github\dj-postgres-heroku\venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\github\dj-postgres-heroku\venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\github\dj-postgres-heroku\venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\github\dj-postgres-heroku\venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\github\dj-postgres-heroku\venv\lib\site-packages\rest_framework\mixins.py", line 19, in create
self.perform_create(serializer)
File "C:\github\dj-postgres-heroku\get_staff\views.py", line 21, in perform_create
serializer.save()
File "C:\github\dj-postgres-heroku\venv\lib\site-packages\rest_framework\serializers.py", line 205, in save
self.instance = self.create(validated_data)
File "C:\github\dj-postgres-heroku\get_staff\serializers.py", line 32, in create
profile_data = validated_data.pop('profile')
KeyError: 'profile'
As you add profile field read_only=True thats why validated_data doesn't have profile key.
Provode profile primary key in request body and remove read_only=True from profile field.
Related
halo i'm working on a project that requires user to give a feedback whenever possible using django rest_framework
but im getting an some difficulties doing that
below is my code snippet & err msg
##mode file
class Review(models.Model):
school = models.ForeignKey(
Profile, on_delete=models.CASCADE, related_name='review')
name = models.CharField(max_length=250, blank=True, null=True)
reviewer_email = models.EmailField()
rating = models.CharField(
max_length=250, blank=True, null=True)
review = models.TextField()
##serializer file
class ReviewSerializer(serializers.ModelSerializer):
class Meta:
model = Review
fields = ('name', 'review', 'id', 'reviewer_email', 'rating')
def perform_create(self, serializer):
id = self.request
print(self.request.user.profile)
serializer.save(school=self.request.user.profile.school_id)
##apiView
class ReviewAPIView(generics.CreateAPIView):
serializer_class = ReviewSerializer
permissions = [permissions.AllowAny]
queryset = Review.objects.all()
err msg
File "/home/olaneat/Desktop/files/project/django/schMrk/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/olaneat/Desktop/files/project/django/schMrk/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/olaneat/Desktop/files/project/django/schMrk/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/olaneat/Desktop/files/project/django/schMrk/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/olaneat/Desktop/files/project/django/schMrk/lib/python3.8/site-packages/rest_framework/generics.py", line 190, in post
return self.create(request, *args, **kwargs)
File "/home/olaneat/Desktop/files/project/django/schMrk/lib/python3.8/site-packages/rest_framework/mixins.py", line 19, in create
self.perform_create(serializer)
File "/home/olaneat/Desktop/files/project/django/schMrk/sch-market/schoolDetail/apiviews.py", line 213, in perform_create
print(self.request.user.profile)
AttributeError: 'AnonymousUser' object has no attribute 'profile'
[04/Aug/2021 16:00:59] "POST /school-detail/add-review HTTP/1.1" 500 105554
can anyone help pls
You didn't share the request body.
My guess(from the error) is that you didn't specify the id of the school object it should be related to
You should limit access for non-authorized users. Otherwise, self.request.user could be an AnonymousUser object, which has no a profile relation.
permissions = [permissions.IsAuthenticated]
I am trying to list and paginate favourite post of users. I got a custom pagination snippet from so, which I am using here, But I am getting this error.
ValueError:Cannot use QuerySet for "Post": Use a QuerySet for "Account".
I guess, there are problems with my serializer or views. can anyone please help me fix this issue?
These are the codes:
serializers:
class PostSerializer(serializers.ModelSerializer):
user = serializers.ReadOnlyField(source='user.username')
post_date = serializers.ReadOnlyField()
comment_set = CommentSerializer(many=True)
class Meta:
model = Post
fields = ['id','title', 'post_date', 'user', 'image', 'comment_set']
class PostFavouriteListSerializer(serializers.ModelSerializer):
favourite = PostSerializer(many=True, read_only=True)
class Meta:
model = Post
fields = ['favourite',]
views
class CustomPaginator(PageNumberPagination):
page_size = 3
def generate_response(self, query_set, serializer_obj, request):
try:
page_data = self.paginate_queryset(query_set, request)
except NotFoundError:
return Response({"error": "No results found for the requested page"}, status=status.HTTP_400_BAD_REQUEST)
serialized_page = serializer_obj(page_data, many=True)
return self.get_paginated_response(serialized_page.data)
class FavouritePostAPIView(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
def get(self, request):
user = self.request.user
favourite_posts = Post.objects.filter(favourite=user.post_favourite.all())
paginator = CustomPaginator()
response = paginator.generate_response(post, PostFavouriteListSerializer, request)
return response
This is the Post model:
class Post(models.Model):
title = models.TextField(max_length=5000, blank=False, null=False)
image = models.ImageField(upload_to='posts/postimage/', null=True)
post_date = models.DateTimeField(auto_now_add=True, verbose_name="Date Posted")
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
slug = models.SlugField(blank=True, unique=True, max_length=255)
favourite = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='post_favourite', blank=True)
Traceback:
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\views.py" in dispatch
505. response = self.handle_exception(exc)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\views.py" in handle_exception
465. self.raise_uncaught_exception(exc)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\views.py" in raise_uncaught_exception
476. raise exc
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\views.py" in dispatch
502. response = handler(request, *args, **kwargs)
File "C:\danny\Project\posts\api\views.py" in get
186. favourite_posts = Post.objects.filter(favourite=user.post_favourite.all())
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py" in filter
892. return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
910. clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\query.py" in add_q
1290. clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\query.py" in _add_q
1315. child_clause, needed_inner = self.build_filter(
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\query.py" in build_filter
1224. self.check_related_objects(join_info.final_field, value, join_info.opts)
File "C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\query.py" in check_related_objects
1079. raise ValueError(
Exception Type: ValueError at /api/posts/posts/favourites/
Exception Value: Cannot use QuerySet for "Post": Use a QuerySet for "Account".
replace :
favourite_posts = Post.objects.filter(favourite=user.post_favourite.all())
paginator = CustomPaginator()
response = paginator.generate_response(post, PostFavouriteListSerializer, request)
by :
favourite_posts = user.post_favourite.all()
paginator = CustomPaginator()
response = paginator.generate_response(favourite_posts, PostSerializer, request)
the related m2m post_favourite is a queryset of post , not need to filter/get this, so you dont need PostFavouriteListSerializer
alternative can be this:
favourite_posts = Post.objects.filter(favourite=user)
you get all post with favourite have your user
I've read a bunch of similar sounding questions of SO, but somehow none of them have helped me solve my particular case. I added a ManyToManyField to represent "Likes" to my Photo model:
class Photo(TimestampModerated):
owner = models.ForeignKey('auth.User', related_name='photos', on_delete=models.CASCADE)
uuid = models.UUIDField(default=uuid4, editable=False)
slug = models.SlugField(max_length=80, editable=False)
title = models.CharField(max_length=80, blank=False, null=False)
description = models.TextField(verbose_name='description of entity', blank=True, null=True)
photo = models.ImageField(upload_to=user_directory_path, height_field="height", width_field="width", blank=True)
height = models.IntegerField(blank=True)
width = models.IntegerField(blank=True)
tags = TaggableManager(blank=True)
hash = models.CharField(max_length=64, unique=True, null=True)
size = models.BigIntegerField('size of file in bytes', blank=True, null=True)
likes = models.ManyToManyField('auth.User', blank=True, null=True)
class Meta:
verbose_name_plural = "photos"
def __str__(self):
return self.title
def delete(self, using=None, keep_parents=False):
default_storage.delete("{}".format(self.photo))
super().delete()
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Photo, self).save(*args, **kwargs)
Here is the view (at least the part that should matter for creating):
class PhotoViewSet(viewsets.ModelViewSet):
queryset = Photo.objects.all()
serializer_class = PhotoSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsOwnerOrReadOnly, permissions.AllowAny,)
parser_classes = (MultiPartParser, FormParser)
def perform_create(self, serializer):
serializer.save(owner=self.request.user, likes=[])
And here is the serializer:
class PhotoSerializer(serializers.ModelSerializer, TaggitSerializer):
owner = serializers.CharField(source='owner.username', read_only=True)
tags = TagListSerializerField()
photo = Base64ImageField(
max_length=None, use_url=True,
)
class Meta:
model = Photo
fields = ('photo', 'height', 'width', 'owner', 'slug', 'uuid', 'title', 'id', 'created', 'updated',
'moderation_code', 'tags', 'hash', 'description', 'size', 'likes')
def create(self, validated_data):
photo = Photo.objects.create(owner=validated_data.pop('owner'),
**validated_data)
p = Photo.objects.get(uuid=photo.uuid)
[p.tags.add(tag) for tag in validated_data['tags']]
return photo
To be clear, the only thing that has been added is the likes field in the model. Also, I should note that I am able to actually POST new likes to photos that already existed in the DB before the migration. I'm just having issues creating new instances. Any ideas?
Here is the full error traceback:
Internal Server Error: /api/photos/
Traceback (most recent call last):
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/rest_framework/viewsets.py", line 86, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/rest_framework/views.py", line 489, in dispatch
response = self.handle_exception(exc)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/rest_framework/views.py", line 449, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/rest_framework/views.py", line 486, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/rest_framework/mixins.py", line 21, in create
self.perform_create(serializer)
File "/Users/xxxxxx/_/photos/views.py", line 24, in perform_create
serializer.save(owner=self.request.user, likes=[])
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 215, in save
self.instance = self.create(validated_data)
File "/Users/xxxxxx/_/photos/serializers.py", line 88, in create
**validated_data)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/django/db/models/query.py", line 392, in create
obj = self.model(**kwargs)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/django/db/models/base.py", line 566, in __init__
_setattr(self, prop, kwargs[prop])
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 536, in __set__
manager = self.__get__(instance)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 513, in __get__
return self.related_manager_cls(instance)
File "/Users/xxxxxx/_/.virtualenv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 830, in __init__
(instance, self.pk_field_names[self.source_field_name]))
ValueError: "<Photo: photo-260561.jpeg>" needs to have a value for field "id" before this many-to-many relationship can be used.
Well, I solved this with one seemingly very simple line of code:
def create(self, validated_data):
validated_data.pop('likes') ## adding this line solved the issue
photo = Photo.objects.create(owner=validated_data.pop('owner'),
**validated_data)
p = Photo.objects.get(uuid=photo.uuid)
[p.tags.add(tag) for tag in validated_data['tags']]
return photo
If anyone can explain why this works, I'd be interested to know. I should point out that for this use case a photo would never be initiated with likes.
In django rest framework If i use DictField() in serializer its given me AttributeError at /api/v1/analytics/reports-configuration/1/
‘str’ object has no attribute ‘items’ error.please take a look.
serializers.py
import json
from rest_framework import serializers
from models import ReportConfiguration
class ReportConfigurationSerializer(serializers.ModelSerializer):
report_type = serializers.ChoiceField(choices=['query', 'activity', 'summary'])
read_only = serializers.BooleanField(required=True)
query_data = serializers.DictField(required=True)
class Meta:
model = ReportConfiguration
fields = ('id', 'title', 'query_data', 'created_by', 'last_activity_by', 'deleted',
'deleted_by', 'added_at', 'last_activity_at', 'report_type', 'read_only')
def validate(self, data):
if data['report_type'] is not 'summary' and data['read_only'] is True:
raise serializers.ValidationError("If read only true then report type should be summary")
if data['report_type'] is 'summary' and data['read_only'] is not True:
raise serializers.ValidationError("If read only not true then report type should not be summary")
return data
def create(self, validated_data):
validated_data['created_by'] = self.context['request'].user
validated_data['last_activity_by'] = self.context['request'].user
return ReportConfiguration.objects.create(**validated_data)
views.py
class ReportConfigurationViewSet(viewsets.ModelViewSet):
serializer_class = ReportConfigurationSerializer
queryset = ReportConfiguration.objects.all()
permission_classes = (permissions.IsAuthenticated,)
filter_backends = (DjangoFilterBackend,)
filter_class = ReportFilter
def list(self, request):
response = super(ReportConfigurationViewSet, self).list(request)
for data in response.data['results']:
data['query_data'] = json.loads(data['query_data'])
return response
def retrieve(self, request, pk=None):
response = super(ReportConfigurationViewSet, self).retrieve(request, pk=pk)
response.data['query_data'] = json.loads(response.data['query_data'])
return response
models.py
class ReportConfiguration(models.Model):
class ReportTypeChoice(Choice):
QueryReport = 'query'
ActivityReport = 'activity'
SummaryList = 'summary'
title = models.CharField(max_length=300, unique=True, blank=False, null=False)
query_data = JSONField(default={})
created_by = models.ForeignKey(User, null=True, blank=True, related_name='report_created_by')
last_activity_by = models.ForeignKey(User, null=True, blank=True,related_name='last_active_in_report')
deleted = models.BooleanField(default=False)
deleted_by = models.ForeignKey(User, null=True, blank=True, related_name='report_deleted_by')
added_at = models.DateTimeField(auto_now_add=True)
last_activity_at = models.DateTimeField(default=timezone.now)
report_type = models.CharField(max_length=15, choices=ReportTypeChoice)
read_only = models.BooleanField(default=False)
Error:
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
ERROR Internal Server Error: /api/v1/analytics/reports-configuration/1/
Traceback (most recent call last):
File "/Users/ranvijay.s/.virtualenvs/my_project/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/ranvijay.s/.virtualenvs/my_project/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/ranvijay.s/.virtualenvs/my_project/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/ranvijay.s/.virtualenvs/my_project/lib/python2.7/site-packages/rest_framework/viewsets.py", line 83, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/ranvijay.s/.virtualenvs/my_project/lib/python2.7/site-packages/rest_framework/views.py", line 477, in dispatch
response = self.handle_exception(exc)
File "/Users/ranvijay.s/.virtualenvs/my_project/lib/python2.7/site-packages/rest_framework/views.py", line 437, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/ranvijay.s/.virtualenvs/my_project/lib/python2.7/site-packages/rest_framework/views.py", line 474, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/ranvijay.s/wk/project/django/dhanalytics/views.py", line 47, in retrieve
response = super(ReportConfigurationViewSet, self).retrieve(request, pk=pk)
File "/Users/ranvijay.s/.virtualenvs/my_project/lib/python2.7/site-packages/rest_framework/mixins.py", line 58, in retrieve
return Response(serializer.data)
File "/Users/ranvijay.s/.virtualenvs/my_project/lib/python2.7/site-packages/rest_framework/serializers.py", line 527, in data
ret = super(Serializer, self).data
File "/Users/ranvijay.s/.virtualenvs/my_project/lib/python2.7/site-packages/rest_framework/serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "/Users/ranvijay.s/.virtualenvs/my_project/lib/python2.7/site-packages/rest_framework/serializers.py", line 496, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "/Users/ranvijay.s/.virtualenvs/my_project/lib/python2.7/site-packages/rest_framework/fields.py", line 1601, in to_representation
for key, val in value.items()
AttributeError: 'str' object has no attribute 'items'
I want to use the rest framework api to render the json data from the list method and the form from the post in a template. I have been searching in the rest-framework docs for the renderer clases, Html rendering and form rendering but came to no solution. This is the relevant code
//models.py
class Collaborator(models.Model):
user = models.OneToOneField(User,
related_name='collaborator',
on_delete=models.CASCADE
)
area = models.ForeignKey(Area,
verbose_name=_('Área del colaborador'),
related_name='collaborator'
)
position = models.ForeignKey(Position,
verbose_name=_('Cargo del Colaborador'),
related_name='collaborator'
)
photo = models.ImageField(_('Fotografía del colaborador'),
null=True,
blank=True,
help_text=_('Fotografía del Colaborador'),
)
birth_date = models.DateField(_('Fecha de Nacimiento'),
null=True,
blank=True
)
join_date = models.DateField(_('Fecha de Ingreso'),
null=True,
blank=True
)
payment = models.DecimalField(_('Sueldo'),
max_digits=8,
decimal_places=4,
null=True,
blank=True
)
fire_reason = models.TextField(_('Razón de despido / renuncia'),
blank=True,
null=True,
help_text=_('Detallar la razón de por qué salió el colaborador.')
)
class Meta:
ordering = (
'id',
'user',
'area',
'birth_date',
'join_date',
'payment',
)
verbose_name = _('Colaborador')
verbose_name_plural = _('Colaboradores')
def __unicode__(self): # __unicode__ on Python 2
return '%s %s' % (self.user.first_name,
self.user.last_name
)
//serializers.py
class UserSerializer(ModelSerializer):
class Meta:
model = User
fields = (
'username',
'password',
'first_name',
'last_name',
'email',
'groups',
'is_active'
)
class CollaboratorSerializer(ModelSerializer):
user = UserSerializer(many=False)
class Meta:
model = Collaborator
fields = (
'id',
'user',
'photo',
'area',
'position',
'birth_date',
'payment',
'join_date',
'fire_reason',
)
position = StringRelatedField(many=False)
area = StringRelatedField(many=False)
def create(self, validated_data):
user_data = validated_data.pop('user')
user = User.objects.create(**validated_data)
User.objects.create(user=user, **user_data)
return user
//views (rest framework view)
class CollaboratorViewSet(viewsets.ModelViewSet):
"""
A viewset for viewing and editing user instances.
"""
serializer_class = CollaboratorSerializer
queryset = Collaborator.objects.all()
//urls.py
urlpatterns = [
url(r'^collaborators-list/$',
CollaboratorViewSet.as_view({'get':'list','post':'create','delete':'destroy','put':'update'}),
name='collaborators-list'
),
]
This is the traceback error
Traceback (most recent call last):
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\core\handlers\exception.py", line 42, in inner
response = get_response(request)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\core\handlers\base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\core\handlers\base.py", line 215, in _get_response
response = response.render()
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\template\response.py", line 109, in render
self.content = self.rendered_content
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\rest_framework\response.py", line 72, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\rest_framework\renderers.py", line 176, in render
return template_render(template, context, request=request)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\rest_framework\compat.py", line 332, in template_render
return template.render(context, request=request)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\template\backends\django.py", line 64, in render
context = make_context(context, request, autoescape=self.backend.engine.autoescape)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\template\context.py", line 267, in make_context
context.push(original_context)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\template\context.py", line 59, in push
return ContextDict(self, *dicts, **kwargs)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\template\context.py", line 18, in __init__
super(ContextDict, self).__init__(*args, **kwargs)
ValueError: dictionary update sequence element #0 has length 9; 2 is required
[27/Apr/2017 08:45:30] "GET /es/gdh/collaborators-list/ HTTP/1.1" 500 107315
Edit 2
Removing this from the url {'get':'list','post':'create','delete':'destroy','put':'update'}, I get this error.
Traceback (most recent call last):
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\core\handlers\exception.py", line 42, in inner
response = get_response(request)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\utils\deprecation.py", line 138, in __call__
response = self.process_response(request, response)
File "D:\PROGRAMACION\VENVS\gutenberg\lib\site-packages\django\middleware\clickjacking.py", line 32, in process_response
if response.get('X-Frame-Options') is not None:
AttributeError: 'function' object has no attribute 'get'
[27/Apr/2017 09:47:21] "GET /es/gdh/collaborators-list/ HTTP/1.1" 500 63775