如何在django模型中使用外键获取对象的所有属性
How to get all the properties of objects using foreign key in django model
我使用 Djangorestframework 创建了 rest api。我在我的应用国家和州有两个模型。我使用外键方法将相关国家模型转换为状态模型,但是在获取状态 api 中的状态列表时,我正在获取状态名称,但在国家的地方我正在获取国家主键 ID 而不是它的名称如何我可以获取国家/地区的所有字段而不是 PK id
----------
Models.py code
class countries(models.Model):
country = models.CharField(max_length=10)
def __str__(self):
return self.country
class states(models.Model):
state = models.CharField(max_length=15)
country = models.ForeignKey(countries, on_delete=models.PROTECT)
def __str__(self):
return self.state
----------
Serializers.py code
from rest_framework import serializers
from .models import countries, states
class countiresSerializers(serializers.ModelSerializer):
class Meta:
model = countries
fields = '__all__'
class statesSerializers(serializers.ModelSerializer):
class Meta:
model = states
fields = '__all__'
----------
Viewset.py code--
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from.models import countries, states
from .serializers import countiresSerializers, statesSerializers
class countryList(APIView):
def get(self, request):
country1 = countries.objects.all()
serializer = countiresSerializers(country1, many=True)
return Response (serializer.data)
def __pos__(self):
pass
class statesList(APIView):
def get(self, request):
state = states.objects.all()
serializer = statesSerializers(state, many=True)
return Response (serializer.data)
def __pos__(self):
pass
我附上了图片,你可以看到显示主要 ID 而不是名称的国家/地区,我如何获得名称和国家/地区的所有其他相关字段..
您可以使用 depth
序列化程序的选项:
class statesSerializers(serializers.ModelSerializer):
class Meta:
model = states
fields = '__all__'
depth = 1
我使用 Djangorestframework 创建了 rest api。我在我的应用国家和州有两个模型。我使用外键方法将相关国家模型转换为状态模型,但是在获取状态 api 中的状态列表时,我正在获取状态名称,但在国家的地方我正在获取国家主键 ID 而不是它的名称如何我可以获取国家/地区的所有字段而不是 PK id
----------
Models.py code
class countries(models.Model):
country = models.CharField(max_length=10)
def __str__(self):
return self.country
class states(models.Model):
state = models.CharField(max_length=15)
country = models.ForeignKey(countries, on_delete=models.PROTECT)
def __str__(self):
return self.state
----------
Serializers.py code
from rest_framework import serializers
from .models import countries, states
class countiresSerializers(serializers.ModelSerializer):
class Meta:
model = countries
fields = '__all__'
class statesSerializers(serializers.ModelSerializer):
class Meta:
model = states
fields = '__all__'
----------
Viewset.py code--
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from.models import countries, states
from .serializers import countiresSerializers, statesSerializers
class countryList(APIView):
def get(self, request):
country1 = countries.objects.all()
serializer = countiresSerializers(country1, many=True)
return Response (serializer.data)
def __pos__(self):
pass
class statesList(APIView):
def get(self, request):
state = states.objects.all()
serializer = statesSerializers(state, many=True)
return Response (serializer.data)
def __pos__(self):
pass
我附上了图片,你可以看到显示主要 ID 而不是名称的国家/地区,我如何获得名称和国家/地区的所有其他相关字段..
您可以使用 depth
序列化程序的选项:
class statesSerializers(serializers.ModelSerializer):
class Meta:
model = states
fields = '__all__'
depth = 1