show value instead of foreign key DJANGO API

Asked

Viewed 23 times

-2

Model

from django.db import models
from django.contrib.auth.models import User

user_agent = models.ForeignKey(User, on_delete=models.CASCADE)

def __str__(self):
   return self.name

VIEWS

from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from django.http.response import JsonResponse

from devedor.models import Debtor, Employees

from devedor.serializers import DebtorSerializer, EmployeesSerializer

from django.core.files.storage import default_storage


##def

@csrf_exempt
def debtorAPI(request,id=0):
    if request.method=='GET':
        debtors = Debtor.objects.all()
        debtors_serializer = DebtorSerializer(debtors, many= True)
        return JsonResponse(debtors_serializer.data, safe=False)

Serializers

from django.db.models import fields
from rest_framework import serializers

from devedor.models import Debtor, Employees

class DebtorSerializer(serializers.ModelSerializer):
   class Meta:
       model = Debtor
       fields = (
                'IdDebtor',
                'name',
                'id_tax',
                'id_espaider',
                'contabil',
                'risco',
                'wallet_type',
                'phone_1',
                'phone_2',
                'phone_hot',
                'phone_whatsapp',
                'status_negociacao',
                'user_agent',
                'cpc_status',
                'data_alteracao',
                'observacoes_status',
                'cpc_status',
                'observacoes_status'
       )

IS RETURNING

  {
        "IdDebtor": 1,
        "name": "William Marques",
        "id_tax": "001.255.481-29",
        "id_espaider": "21223",
        "contabil": "53452344.00",
        "risco": "4654361.00",
        "wallet_type": "MASSIFICADO PF",
        "phone_1": "51991736242",
        "phone_2": "51991736242",
        "phone_hot": "51991736242",
        "phone_whatsapp": "51991736242",
        "status_negociacao": "PROPOSTA APROVADA",
        **"user_agent": 3,**  PRECISO RETORNAR O NOME ----
        "cpc_status": "SIM",
        "data_alteracao": "2021-08-24T18:51:01.235021Z",
        "observacoes_status": "paris to paris"
    }
]

1 answer

0

To get information from another model, you can nest the serializers. Nesting serializers is easier than it looks. Let me give you a general example, and you can go deeper in that documentation.

Nesting is when you have a model, which has to be "called", in the serialization of another model.

The serializer of the model that must be "pulled" must be declared before. Just below it, the serializer of the model you want to use as the main one, should call the top serializer (from the model to be "pulled"), and add it to the Fields. This example comes from the documentation.

class TrackSerializer(serializers.ModelSerializer):
    class Meta:
        model = Track
        fields = ['order', 'title', 'duration']

class AlbumSerializer(serializers.ModelSerializer):
    tracks = TrackSerializer(many=True, read_only=True)


    class Meta:
        model = Album
        fields = ['album_name', 'artist', 'tracks']

Many serves to receive more than one value, and read_only can be disabled if you create and update your templates within the serializer.

See, the Albumserializer "calls" the secondary serializer, the response of this serializer will be included as a normal field. Like I said, it’s a general idea, but it can be applied to your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.