Django swagger api returned object url instead of readable name

I have an model which is for mapping book(item) to categories(tag), it shows like this in the django admin page.

id      item_uid     tag_uid
407     Food         Recipe

but in django swagger page, when I try to GET this mapping api with ID 407, it returned like this:

"id": 407,
"item_uid": "";
"tag_uid": ""

as you can see, it mapped together correctly, but the response body showed the object url and it's object id, which is not readable for human users. I wonder that if there is anyway to make them like this:

"id": 407,
"item_uid": "Food";
"tag_uid": "Recipe"

Thank you!

It seems you are using a HyperlinkedModelSerializer instead of a regular ModelSerializer

Try changing the serializer class to a ModelSerializer:

class Map_item_tag_Serializer(serializers.ModelSerializer):
     class Meta:
         model = Map_item_tag
         fields = ['id','item_uid','tag_uid']

In addition, I would advise you to use CamelCase notation for all your classes. For example: instead of using Map_item_tag_Serializer, change the name to MapItemTagSerializer. The same goes for all your other classes.

I would also avoid using using the _uuid suffix when using ForeignKey relationships. In the MapItemTag model, the ForeignKey relationship inherently means that the field will point to an object Item of Tag object. Hence, no need to specify the _uuid part again.

For example, the following changes would make the model a lot more readable:

class MapItemTag(models.Model):
 item = models.ForeignKey(Item, on_delete=models.CASCADE, verbose_name='map_item')
 tag = models.ForeignKey(Tag, on_delete=models.CASCADE, verbose_name='map_tag')
Back to Top