Handle image upload with multipart form and nested data

I have the standard Django User model and a Profile model that has a OneToOne with the User. So the payload for creating the whole thing at once would be:

# models.py
class Profile(models.Model):
  user = models.OneToOneField(User)
  profile_picture = models.ImageField()
  # Other extra profile related information

# serializers.py
class ProfileSerializer(serializers.ModelSerializer):
  user = UserSerializer()
  class Meta:
    model = Profile
    fields = "__all__"
{
  "user": { "first_name": "", "last_name": ""},
  "profile_picture": <file>
}

this of course is sent as a multipart form since it contains image upload. But then you cannot send nested data with multipart form, I guess? I have write extra code to make it work. What's the standard? Should it be created in multi step requests? So create a user, then create profile and attach image?

Please let me know if you require further clarification. I understand my question isn't too clear.

Вернуться на верх