jazzband / django-taggit

Simple tagging for django
https://django-taggit.readthedocs.io
BSD 3-Clause "New" or "Revised" License
3.33k stars 622 forks source link

Wish to have more detail in serializer, looking for tags should be nested with all the items #858

Open MicroDreamIT opened 1 year ago

MicroDreamIT commented 1 year ago

here is my Model is look like

model.py

class Event(models.Model):
    title = models.CharField(max_length=151, db_index=True)
    description = models.TextField(blank=True, null=True)
    tags = TaggableManager()

serializers.py

class EventSerializer(serializers.ModelSerializer):
    slug = serializers.SerializerMethodField()
    tags = TagListSerializerField()

the Result I am having:

{
            "id": 52,
            "slug": "52-aperiam-amet",
            "tags": [
                "hello",
                "world"
            ],
            "title": "Aperiam amet",
            "description": "Quibusdam ipsum sun Quibusdam ipsum sun Quibusdam ipsum sun ",
        },

but expected tags nest should look like

"tags": [
                {
                   id:"1",
                   slug:'1-hello',
                   name:"hello"
                },
                {
                   id:"2",
                   slug:'2-world',
                   name:"world"
                },
            ],
rtpg commented 1 year ago

I'm not going to outright close this because it might make sense to have some extra serializers, but we can't change the existing one.

I think it makes sense to have both, because in many use cases you don't want to expose the tag ID.

Let's keep this open and maybe we can have someone contribute a more detailed serializer for those who want it :+1: At the very least we can write a very small doc guide to just point out how to easily do this with DRF

rtpg commented 3 months ago

I think the way forward here would be: