[Answer]-JSON from django rest Framework

1👍

This is how your serializer should be defined:

class EstablecimientoSerializer(serializers.ModelSerializer):
    categoria = serializers.RelatedField()

    class Meta:
        model = Establecimiento
        depth = 1
        fields = ('nombre', 'categoria',)

and the viewset:

class EstablecimientoViewSet(viewsets.ModelViewSet):
    queryset = Establecimiento.objects.only('nombre','categoria',)
    serializer_class = EstablecimientoSerializer

This assumes that you have defined __unicode__ method for Categoria:

class Categoria(models.Model):
    # fields..

    def __unicode__(self):
        return self.titulo

If you don’t want to define __unicode__, you can instead of RelatedField use the SlugRelatedField field:

categoria = serializers.SlugRelatedField(read_only=True, slug_field='titulo')

Leave a comment