[Answered ]-Using foreign key as object in Django Telmplates

1πŸ‘

βœ…

I think the code of the serializers isn’t correct.

class ChannelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Channel
        fields = '__all__'

class CategorySerializer(serializers.ModelSerializer):
    channel = ChannelSerializer(many = True, read_only = True)

    class Meta:
        model = Category
        fields = '__all__'

And in the html, some attributes don’t exist in the models. channel_set should be changed into channel and channel.title into channel.channel.
There are no names like channel_set, title in the models.

...
<ul>
    <div>
        {% for channel in category.channel %}
            <li>Teste {{ channel.channel }}</li>                           
        {% endfor %}
    </div>
</ul>
...

Or you should modify the names in the models.

πŸ‘€Metalgear

Leave a comment