In my models.py, there are two model, the AvailableArea has a foreign field refer to AddressRegion:
class AddressRegion(models.Model):
name = models.CharField(max_length=8)
def __str__(self):
return self.name
def __unicode__(self):
return self.name
class AvailableArea(models.Model):
name = models.CharField(max_length=8)
addressregion = models.ForeignKey(AddressRegion, default=1, related_name='availableareas', on_delete=models.CASCADE)
def __str__(self):
return self.name
def __unicode__(self):
return self.name
In the serializers.py, I serialize all the fields:
class AvailableAreaSerializer(ModelSerializer):
"""
可用地区
"""
class Meta:
model = AvailableArea
fields = "__all__"
In the views.py:
class AddressRegionListAPIView(ListAPIView):
serializer_class = AddressRegionSerializer
permission_classes = []
queryset = AddressRegion.objects.all()
The rest framework data is like this:
[
{
"id": 13,
"name": "福州一区",
"addressregion": 3
},
{
"id": 14,
"name": "西南一区",
"addressregion": 4
},
{
"id": 15,
"name": "西南一区",
"addressregion": 3
}
]
I want the addressregion not shows the addressregion's id, but shows the addressregion's name.