Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PUT, PATCH serializer nested with image field django not working, return empty array #144

Open
1000Roses opened this issue May 27, 2021 · 0 comments

Comments

@1000Roses
Copy link

1000Roses commented May 27, 2021

I have adready config MEDIA_URL, MEDIA_ROOT in setting.py

Models.py

from django.db import models

class ColorProduct(models.Model):
    color= models.CharField(max_length=15)
    price= models.PositiveIntegerField()

    def __str__(self):
        return f"{self.color} - {self.price}"

class ImageProduct(models.Model):
    color= models.ForeignKey(ColorProduct, related_name= 'images', on_delete= models.CASCADE)
    image= models.ImageField()

    def __str__(self):
        return f"{self.color} - {self.image}"

Serializers.py

from rest_framework import serializers
from .models import ColorProduct, ImageProduct
from drf_writable_nested.serializers import WritableNestedModelSerializer

class ImageSerializers(serializers.ModelSerializer):
        class Meta:
            model= ImageProduct
            fields= ['image']
    
class ColorSerializers(WritableNestedModelSerializer):
    images= ImageSerializers(many= True)
    class Meta:
        model= ColorProduct
        fields= '__all__'

Views.py

from django.shortcuts import render
from rest_framework.parsers import MultiPartParser, FormParser
from rest_framework import viewsets
from .serializers import ColorSerializers
from .models import ColorProduct

class ProductModelViewSet(viewsets.ModelViewSet):
    parser_classes= [MultiPartParser, FormParser]
    queryset= ColorProduct.objects.all()
    serializer_class = ColorSerializers

When i create, its okay. But update, the images field return empty
Ex: Return

  {
      "id": 3,
      "images": [],
      "color": "darkred",
      "price": 55
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant