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 "[email protected]" 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.

5
  • stackoverflow.com/questions/20473572/… Commented Jun 2, 2015 at 9:58
  • stackoverflow.com/questions/26673572/… Commented Jun 2, 2015 at 9:58
  • stackoverflow.com/questions/20303252/… Commented Jun 2, 2015 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. Commented Jun 2, 2015 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? Commented Jun 3, 2015 at 1:46

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

how to upload multiple files and also the mail at the same time?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.