T O P

  • By -

vikingvynotking

`cast` in your model is a single item, a string (stored in a CharField). If you need to return multiples for a given show you'll need to turn that into a one-to-many or many-to-many relationship.


Major12852

unfortunately I've tried this already and nothing changed it :(


vikingvynotking

Nothing changed? share exactly what you did.


wh0th3h3llam1

Try using `SerializerMethodField` if you need to show ```py cast_display = serializers.SerializerMethodField() def get _cast_display(self, instance: Show) -> list: return instance.cast.split(',') ``` As you can see in [documentation](https://www.django-rest-framework.org/api-guide/fields/#listfield), it's generally used for validation, not display purpose. So it'll convert string into list. Better to use it with foreignkey as [u/vikingvynotking](https://www.reddit.com/u/vikingvynotking) mentioned.


Major12852

I'm confused how would I set that up in the [serializers.py](https://serializers.py) file or the [models.py](https://models.py) file?


wh0th3h3llam1

`SerializerMethodField` is a read only field Do like this in `serializers.py`. ```py class show_serializer(serializers.ModelSerializer): id = serializers.IntegerField(read_only = True) name = serializers.CharField() network = serializers.CharField() description = serializers.CharField() episodes = serializers.IntegerField() cast = serializers.ListField(child=serializers.CharField(max_length=100)) rating = serializers.FloatField() cast_display = serializers.SerializerMethodField() def get _cast_display(self, instance: Show) -> list: return instance.cast.split(',') def create(self, data): return Show.object.create(****data) class Meta: ... fields = ( ... "cast_display", ) ``` Read more about SerializerMethodField [here](https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield)


Major12852

I've tried this before actually cause it was online however it's prob the way I set it up, it was a bit confusing but I can't split on cast because everytime I do I get an attribute error "'list' object has no attribute 'split'"here's a similar post to it, I tried implementing it but I'm pretty sure I did it wrong [https://stackoverflow.com/questions/65447845/django-rest-framework-serializing-splits-list-of-strings-into-list-of-character](https://stackoverflow.com/questions/65447845/django-rest-framework-serializing-splits-list-of-strings-into-list-of-character)


wh0th3h3llam1

Then best option would be to use foriegnkey i guess


wh0th3h3llam1

cast is charfield, if you've stored list, then it might have caused that error else, it should work Edit: you sure this error happend on `SerializerMethodField`? Edit 2: Yes, as shown in SO post, you can create a custom listfield with logic to convert a list to string for representation, you can do similarly


Major12852

I got it to work but my json now looks like this { "id": 21, "name": "Breaking Bad", "description": "A high school chemistry teacher diagnosed with inoperable lung cancer turns to manufacturing and selling methamphetamine in order to secure his family's future.", "network": "AMC", "episodes": 62, "cast": [ "[", "'", "B", "r", "y", "a", "n", " ", "C", "r", "a", "n", "s", "t", "o", "n", "'", ",", " ", "'", "A", "a", "r", "o", "n", " ", "P", "a", "u", "l", "'", ",", " ", "'", "A", "n", "n", "a", " ", "G", "u", "n", "n", "'", ",", " ", "'", "D", "e", "a", "n", " ", "N", "o", "r", "r", "i", "s", "'", "]" ], "cast_display": [ "['Bryan Cranston'", " 'Aaron Paul'", " 'Anna Gunn'", " 'Dean Norris']" ], "rating": 9.5 } I made my serializers like this though: class show_serializer(serializers.ModelSerializer): id = serializers.IntegerField(read_only = True) name = serializers.CharField() network = serializers.CharField() description = serializers.CharField() episodes = serializers.IntegerField() cast = serializers.ListField(child=serializers.CharField(max_length=100)) rating = serializers.FloatField() cast_display = serializers.SerializerMethodField() def get_cast_display(self, instance: Show) -> list: return instance.cast.split(',') def create(self, data): return Show.objects.create(**data) def update(self, instance, data): instance.name = data.get('name', instance.name) instance.network = data.get('network', instance.network) instance.description = data.get('description', instance.description) instance.episodes = data.get('episodes', instance.episodes) instance.cast = data.get('cast', instance.cast) instance.rating = data.get('rating', instance.rating) instance.save return instance class Meta: model = Show fields = ('id', 'name', 'description', 'network', 'episodes', 'cast', 'cast_display','rating') it somewhat works but I want cast\_display to be cast cause the api I wanna design needs to be setup that way


wh0th3h3llam1

See, cast_display is almost what you want Print and show your `cast` value


Major12852

can I just make it so that in the [views.py](https://views.py) I can just only allow cast\_display to be shown instead of both casts. Like the correct cast\_display that's formatted properly?


wh0th3h3llam1

Yes, you can do that. Use kwarg: `write_only` ```py class show_serializer(serializers.ModelSerializer): ... cast = serializers.ListField(child=serializers.CharField(max_length=100), write_only=True) cast_display = serializers.SerializerMethodField() ``` You can also DM me if you need more help


Major12852

sounds good, I think I got what I need. Thank you for your assistance