How to upload multiple files in django rest framework

I manage to solve this issue and I hope it will help community serializers.py: class FileListSerializer ( serializers.Serializer ) : image = serializers.ListField( child=serializers.FileField( max_length=100000, allow_empty_file=False, use_url=False ) ) def create(self, validated_data): blogs=Blogs.objects.latest(‘created_at’) image=validated_data.pop(‘image’) for img in image: photo=Photo.objects.create(image=img,blogs=blogs,**validated_data) return photo class PhotoSerializer(serializers.ModelSerializer): class Meta: model = Photo read_only_fields = (“blogs”,) views.py: class PhotoViewSet(viewsets.ModelViewSet): serializer_class … Read more

Django REST Framework image upload

you can create separate endpoint for uploading images, it would be like that: class ProductViewSet(BaseViewSet, viewsets.ModelViewSet): queryset = Product.objects.all() serializer_class = ProductSerializer @detail_route(methods=[‘post’]) def upload_docs(request): try: file = request.data[‘file’] except KeyError: raise ParseError(‘Request has no resource file attached’) product = Product.objects.create(image=file, ….) you can go around that solution — update: this’s how to upload from … Read more

How to programmatically call a Django Rest Framework view within another view?

I found the solution for this in the documentation… https://docs.djangoproject.com/en/4.1/ref/class-based-views/mixins/ Hint is from their example here: class AuthorDetail(View): def get(self, request, *args, **kwargs): view = AuthorDisplay.as_view() return view(request, *args, **kwargs) def post(self, request, *args, **kwargs): view = AuthorInterest.as_view() return view(request, *args, **kwargs)

Token Authentication for RESTful API: should the token be periodically changed?

It is good practice to have mobile clients periodically renew their authentication token. This of course is up to the server to enforce. The default TokenAuthentication class does not support this, however you can extend it to achieve this functionality. For example: from rest_framework.authentication import TokenAuthentication, get_authorization_header from rest_framework.exceptions import AuthenticationFailed class ExpiringTokenAuthentication(TokenAuthentication): def authenticate_credentials(self, … Read more

Adding root element to json response (django-rest-framework)

For posterity, below is the final solution. It has grown slightly from the original as it now reformats paginated results as well. Also I should have specified before, that the reason for the JSON root element is for integration with an Ember front end solution. serializer: from rest_framework.serializers import ModelSerializer from api.models import Contact class … Read more

Django Python rest framework, No ‘Access-Control-Allow-Origin’ header is present on the requested resource in chrome, works in firefox

Install the cors-headers package with pip install django-cors-headers Adds to your installed apps INSTALLED_APPS = [ … ‘corsheaders’, … ] Add on your MIDDLEWARE remember to add as being the first in the list MIDDLEWARE = [ ‘corsheaders.middleware.CorsMiddleware’, ‘django.middleware.common.CommonMiddleware’, … ] Before installed apps put this configuration for anyone to access CORS_ORIGIN_ALLOW_ALL=True Or create a … Read more