2

Suppose I have an application to deal with some mail messages with images.

I am not sure how serializer can help me to create an mail object and all related images at the same time (because mail and images belong to different model as mentioned below)

Here are my related codes, sorry that I removed some fields from my actual model to help focus on this question's situation.

models.py

class Mail(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=255)
    content = models.CharField(max_length=255)

class MailImage(models.Model):
    image = models.ImageField(upload_to=get_file_path, blank=True, null=True)
    mail = models.ForeignKey(Mail, related_name='images')

serializers.py

class MailImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = MailImage

class MailSerializer(serializers.ModelSerializer):
    # images = ??? //Can any one help on this?
    class Meta:
        model = Mail

APIView

class SentMailView(APIView):
    permission_classes = ()
    parser_classes = (FileUploadParser,)
    def post(self, request, format=None):
        data = {
            'images': request.data['images'],
            'title': request.data['title'],
            'content': request.data['content'],
        }
        serializer = MailSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return Response({'success': True})
        else:
            return Response({'success': False})

cURL Testing

curl -X POST -H "Content-Type:multipart/form-data" -F "title=test" -F "content=test" -F "images=@Testing.jpg" http://127.0.0.1:8000/api/sent-mail

Also this cURL command only send one picture, it would be appreciate if anyone can revise it to send multiple images for an mail in ONE cURL command.

Thanks for helping.

Pang
  • 512
  • 3
  • 11
  • 31
  • http://stackoverflow.com/questions/20473572/django-rest-framework-file-upload – chandu Jun 02 '15 at 09:58
  • http://stackoverflow.com/questions/26673572/django-rest-framework-upload-file-to-a-method – chandu Jun 02 '15 at 09:58
  • http://stackoverflow.com/questions/20303252/django-rest-framework-imagefield – chandu Jun 02 '15 at 10:00
  • Moving away from the answer, I think there is an design issue in your model. Mails are supposed to have images not images are supposed to have mails. So Mail should have a foreign key to MailImage, not MailImage should have a foreign key to Mail. And if this is set right then the mail object can give all the related images at the same time. – Ishan Anand Jun 02 '15 at 11:32
  • 1
    Thanks for reply. If mail should have foreign key to image, how to make each mail can have multiple images? If many-to-many field, how can ensure each image belongs to one email? – Pang Jun 03 '15 at 01:46

1 Answers1

0
class FileUploadView(views.APIView):
    parser_classes = (FileUploadParser,)

    def put(self, request, filename, format=None):
        file_obj = request.data['file']
        # ...
        # do some stuff with uploaded file
        # ...
        return Response(status=204)

Source: http://www.django-rest-framework.org/api-guide/parsers/#fileuploadparser

chandu
  • 1,053
  • 9
  • 18