I'm using DRF and I'm stuck at creating a nested serializer for many to many realtionship:

197 views
Skip to first unread message

Pratap Padekar

unread,
Jul 24, 2023, 1:44:27 PM7/24/23
to Django REST framework
  1. here are my models:
  2. 10:23 PM
    class StreamPlatfrom(models.Model): name = models.CharField(max_length=100) about = models.CharField(max_length=150, null=True) website = models.URLField() id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True) createdAt = models.DateTimeField(auto_now_add=True) def __str__(self) -> str: return self.name class WatchList(models.Model): title = models.CharField(max_length=150) storyline = models.TextField() active = models.BooleanField() releaseDate = models.DateField() releaseYear = models.IntegerField() stream = models.ManyToManyField(StreamPlatfrom) id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True) createdAt = models.DateTimeField(auto_now_add=True) def __str__(self) -> str: return self.title
    1. and here are my serializer:
    2. 10:23 PM
      # WatchListSerializer class WatchListSerializer(serializers.ModelSerializer): reviews = ReviewSerializer(many=True, read_only=True) class Meta: model = WatchList fields = ("__all__") depth = 1 def create(self, validated_data): stream_data = validated_data.pop('stream', []) print("Stream_data", stream_data) watchlist = WatchList.objects.create(**validated_data) for stream in stream_data: stream_obj = StreamPlatfrom.objects.get(name=stream['name']) watchlist.stream.add(stream_obj) return watchlist # StreamPlatformSerializer class StreamPlatformSerializer(serializers.ModelSerializer): class Meta: model= StreamPlatfrom fields = ("__all__")
    3. 10:23 PM
      For some reason the create method is not working
    4. 10:24 PM
      When I add stream it returns an empty array

Chetan Ganji

unread,
Jul 24, 2023, 4:06:15 PM7/24/23
to django-res...@googlegroups.com
Hey,

Always send properly formatted code. 


class StreamPlatfrom(models.Model):
name = models.CharField(max_length=100)
about = models.CharField(max_length=150, null=True)
website = models.URLField()
id = models.UUIDField(
primary_key=True, default=uuid.uuid4, editable=False, unique=True
)
createdAt = models.DateTimeField(auto_now_add=True)

def __str__(self) -> str:
return self.name

class WatchList(models.Model):
title = models.CharField(max_length=150)
storyline = models.TextField()
active = models.BooleanField()
releaseDate = models.DateField()
releaseYear = models.IntegerField()
stream = models.ManyToManyField(StreamPlatfrom)
id = models.UUIDField(
primary_key=True, default=uuid.uuid4, editable=False, unique=True
)
createdAt = models.DateTimeField(auto_now_add=True)

def __str__(self) -> str:
return self.title


# WatchListSerializer
class WatchListSerializer(serializers.ModelSerializer):
reviews = ReviewSerializer(many=True, read_only=True)

class Meta:
model = WatchList
fields = "__all__"
depth = 1

def create(self, validated_data):
stream_data = validated_data.pop("stream", [])
print("Stream_data", stream_data)
watchlist = WatchList.objects.create(**validated_data)
for stream in stream_data:
stream_obj = StreamPlatfrom.objects.get(name=stream["name"])
watchlist.stream.add(stream_obj)
return watchlist


# StreamPlatformSerializer
class StreamPlatformSerializer(serializers.ModelSerializer):
class Meta:
model = StreamPlatfrom
fields = "__all__"




I dont see   ReviewSerializer  anywhere in the code. 
I dont see a serializer for stream objects either.

Thanks!


Regards,
Chetan Ganji
+91-900-483-4183


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/67e18478-75d0-4ebc-aa81-78b2552bd153n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages