Thank you for responding.
This is what is sent to the DRF endpoint from the frontend:
-----------------------------404193895316728380273212347628
Content-Disposition: form-data; name="category"
1
-----------------------------404193895316728380273212347628
Content-Disposition: form-data; name="body"
post 1
-----------------------------404193895316728380273212347628
Content-Disposition: form-data; name="can_view"
Everybody
-----------------------------404193895316728380273212347628
Content-Disposition: form-data; name="can_comment"
Everybody
-----------------------------404193895316728380273212347628
Content-Disposition: form-data; name="user"
1
-----------------------------404193895316728380273212347628
Content-Disposition: form-data; name="uploaded_images.0"; filename="tumblr_42e2ad7e187aaa1b4c6f4f7e698d03f2_c9a2b230_640.jpg"
Content-Type: image/jpeg
ÿØÿà
This is the Traceback:
Internal Server Error: /api/v1/posts/create/
Traceback (most recent call last):
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/mixins.py", line 19, in create
self.perform_create(serializer)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/mixins.py", line 24, in perform_create
serializer.save()
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/serializers.py", line 212, in save
self.instance = self.create(validated_data)
File "/mnt/500GB/calvin/business/projects/Current/addapost/website/backend/posts/serializers.py", line 50, in create
uploaded_data = validated_data.pop('uploaded_images')
KeyError: 'uploaded_images'
Internal Server Error: /api/v1/posts/create/
Traceback (most recent call last):
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/mixins.py", line 19, in create
self.perform_create(serializer)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/mixins.py", line 24, in perform_create
serializer.save()
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/serializers.py", line 212, in save
self.instance = self.create(validated_data)
File "/mnt/500GB/calvin/business/projects/Current/addapost/website/backend/posts/serializers.py", line 50, in create
uploaded_data = validated_data.pop('uploaded_images')
KeyError: 'uploaded_images'
I’m trying to understand the logic so please bear with me.
This
validated_data.pop('uploaded_images')
removes the last item from a list which in this case would be uploaded_images and I know it must be done so a new Post model can be created without the uploaded_images data. But then if it is removed from the list then this part below cannot function because uploaded images is no longer in the list?
if I remove this part from the serializer’s create method:
try:
for uploaded_item in uploaded_data:
PostImage.objects.create(post = new_post, image = uploaded_item)
except:
PostImage.objects.create(post=new_post)
return new_post
Then the post gets created so i assume the data is valid and the serializer works correctly?
If you look at the data being sent then you will see uploaded_images is the last item BUT I see every uploaded_image has a .0 for the first and .1 for the second and so on appended to the end. Can that be the issue? Why is the 0 and 1 appended and why is it not showing it as [0] since it’s an array?
Your help is appreciated very much
I changed the create part of the serializer to this
def create(self, validated_data):
uploaded_data = validated_data.pop('uploaded_images')
new_post = Post.objects.create(**uploaded_data)
images = validated_data('uploaded_images')
for image in images:
PostImage.objects.create(image=image, post=new_post)
return new_post
because it makes more sense to me. I still get the KeyError
if I do it like this:
def create(self, validated_data):
if 'uploaded_images' in validated_data:
uploaded_data = validated_data.pop('uploaded_images')
new_post = Post.objects.create(**uploaded_data)
images = validated_data('uploaded_images')
for image in images:
PostImage.objects.create(image=image, post=new_post)
return new_post
else:
new_post = Post.objects.create(**validated_data)
return new_post
then the post gets created. Why would the uploaded_images key not be in the dictionary? Does images and uploaded_images have to be fields on the model as well?