0

Hello I want to connect two models through serializers. I have one connection and it works very well, because the primary key belongs to UsuarioDetailSerializer and the foreign key to ComentarioSerializer.

class ComentarioSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        fields = ['id', 'contenido_comentario','fecha_comentario',]
        model = Comentarios

class HabilidadSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        fields = ['id', 'nombre_habilidad',]
        model = Habilidad

class UsuarioDetailSerializer(serializers.ModelSerializer):
    detail = serializers.SerializerMethodField()
    usuario_comentado = ComentarioSerializer(many=True)
    habilidad = HabilidadSerializer()
    class Meta:
        model = Usuario
        fields = [
            'id',
            'id_usuario',
            'nombre_usuario',
            'apellido_usuario',
            'descripcion_usuario',
            'direccion_usuario',
            'foto_usuario',
            'detail',
            'usuario_comentado',
            'habilidad',
        ]
    def get_detail(self, obj):
        return reverse('usuario_detail',args=(obj.pk,))

The other connection that I want to do, is between the serializer UsuarioDetailSerializer with HabilidadSerializer, but I can't do that because the primary key belong to HabilidadSerializer and the foreign key to UsuarioDetailSerializer and I don't know how solving this problem. I want to show the data from HabilidadSerializer into UsuarioDetailSerializer.

  • I finded the solution for muy problem, It's here: https://stackoverflow.com/questions/17280007/retrieving-a-foreign-key-value-with-django-rest-framework-serializers – Gerson Orihuela Maita Mar 12 '21 at 02:24

1 Answers1

0

Once Try This.

class UsuarioDetailSerializer(serializers.ModelSerializer):
    detail = serializers.SerializerMethodField()
    usuario_comentado = ComentarioSerializer(many=True)
    habildad = serializers.PrimaryKeyRelatedField(queryset=habilidad.objects.all())     
    class Meta:
        model = Usuario
        fields = [
            'id',
            'id_usuario',
            'nombre_usuario',
            'apellido_usuario',
            'descripcion_usuario',
            'direccion_usuario',
            'foto_usuario',
            'detail',
            'usuario_comentado',
            'habilidad',
        ]
    def get_detail(self, obj):
        return reverse('usuario_detail',args=(obj.pk,))
lord stock
  • 1,191
  • 5
  • 32
  • Thank you, but that I want to do is showed the data from the model with primary key into the model with foreign key, in another words i want to do the opposite of the documentation examples showing, where the data from model with foreign key is shown into the model with primary key. – Gerson Orihuela Maita Mar 11 '21 at 00:57
  • inside class Meta: add "depth = 1" this will return the foreign key fields as full data objects @GersonOrihuelaMaita – Trent Mar 11 '21 at 01:48
  • Thank you @Trent, but when I want to show the data I have a error, it is: **Could not resolve URL for hyperlinked relationship using view name "habilidad-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.** it's like requiring me to do some kind of serialization. – Gerson Orihuela Maita Mar 11 '21 at 03:21