Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MicroDreamIT opened this issue Jun 19, 2023 · 2 comments

Comments

@MicroDreamIT
Copy link

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"
                },
            ],
@MicroDreamIT MicroDreamIT changed the title Wish to have more detail in serializer Wish to have more detail in serializer, looking for tags should be nested with all the items Jun 19, 2023
@rtpg
Copy link
Contributor

rtpg commented Jul 24, 2023

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 👍 At the very least we can write a very small doc guide to just point out how to easily do this with DRF

@rtpg
Copy link
Contributor

rtpg commented Jun 23, 2024

I think the way forward here would be:

  • Have a DetailedTaggedItemSerializer that could render out a TaggedItem as {name: "foo bar", slug: "foo_bar", id: 1}
  • Have a DetailedTagListField that would probably just be a ListField that uses DetailedTaggedItemSerializer in the child
  • have some test cases involving canonical API interactions (maybe we just implement read-only stuff for now and not. try to resolve create/update first? I dunno)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants