Upload multiple images and link them to property

I am using django rest framework for rest api. I am trying to work with loading multiple images and linking those multiple images to property. However, my viewable api does not show the form to upload images, so I can check if my code is working or not. How do I upload multiple images and link them to a property?

class Property(models.Model):
  owner = models.ForeignKey(settings.AUTH_USER_MODEL)
  address = models.CharField(_('Address'), max_length=140)
  rooms = models.PositiveSmallIntegerField(_('Rooms'))

class Gallery(models.Model):
    property = models.ForeignKey(Property, related_name="gallery")
    caption = models.CharField(null=True, blank=True, max_length=80)
    image = models.ImageField(upload_to="properties/rooms/")

class GallerySerializer(serializers.ModelSerializer):
    class Meta:
        model = Gallery
        fields=('id', 'caption', 'image', )

class PropertySerializer(serializers.ModelSerializer, EagerLoadingMixin):
    _SELECT_RELATED_FIELDS = ['owner', ]
    _PREFETCH_RELATED_FIELDS = ['property_type',]
    gallery = GallerySerializer(many=True)


class PropertyGallery(APIView):
    serializer_class = GallerySerializer
    parser_classes = (FileUploadParser,)
    def put(self, request, property_id=None, format=None):
        serializer = self.serializer_class(data=request.data, partial=True)
        if not serializer.is_valid():
            return Response(serializer.errors, status= status.HTTP_400_BAD_REQUEST)
        else:
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)

url(r'^upload/images/(?P<property_id>[0-9]*)/$', views.PropertyGallery.as_view(), name='property-gallery'),

      

enter image description here

+3


source to share


2 answers


If you remove the line,

parser_classes = (FileUploadParser,)

      

the fields of the serializer will be displayed.



Since you wrote a view that updates existing objects, you may need to throw an exception if the object does not exist with a specific identifier. To do this, you can use the quick access function get_object_or_404()

.

from django.shortcuts import get_object_or_404

def put(self, request, property_id=None, format=None):
    _property = get_object_or_404(Property, id=property_id)
    serializer = self.serializer_class(data=request.data, partial=True)
    if not serializer.is_valid():
        return Response(serializer.errors, status= status.HTTP_400_BAD_REQUEST)
    else:
        serializer.save(property=_property)
        return Response(serializer.data, status=status.HTTP_200_OK)

      

+1


source


I am posting a solution to link multiple images to property. This is working code, but the code requires much more attention. Please feel free to make the following code better.



class GallerySerializer(serializers.ModelSerializer):
    class Meta:
        model = Gallery
        fields=('id', 'caption', 'image', )

    def perform_create(self, serializer):
        print ('serializer', serializer)
        serializer.save(property_instance_id=serializer.validated_data['property_id']) #property_instance_id is because i have changed the name property to property_instance in Gallery model as property is the reserved keyword

class PropertyGallery(APIView):
    serializer_class = GallerySerializer
    parser_classes = (FormParser, MultiPartParser, )
    def put(self, request, property_id=None, format=None):
        serializer = self.serializer_class(data=request.data, partial=True)
        if not serializer.is_valid():
            return Response(serializer.errors, status= status.HTTP_400_BAD_REQUEST)
        else:
            serializer.save(property_instance_id=property_id)
            return Response(serializer.data, status= status.HTTP_200_OK)

      

+1


source







All Articles