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

TypeError: Object of type _TaggableManager is not JSON serializable #839

Open
mirodil1 opened this issue Dec 27, 2022 · 3 comments
Open

Comments

@mirodil1
Copy link

mirodil1 commented Dec 27, 2022

I am using django-taggit, django-parler, django-parler-rest and django-rest-framework on my project and getting following errors when trying to get or add object.

  • When I create object on admin page it is not accepting tags and leaves field empty, if I create tags manually getting errorValueError: language_code can't be null, use translation.activate(..) when accessing translated models outside the request/response loop.
  • When trying to get objects through API, getting error TypeError: Object of type _TaggableManager is not JSON serializable

models.py

class Post(TimeStampedModel):
    translations = TranslatedFields(
      name = models.CharField(
          max_length=255,
          db_index=True,
      ),
      slug = models.SlugField(
          max_length=255,
          db_index=True,
          unique=True,
      )
      tags = TaggableManager(),
    )

serializer.py

class PostSerializer(TaggitSerializer, TranslatableModelSerializer):
    tags = TagListSerializerField()
    translations = TranslatedFieldsField(shared_model=Posts)
    
    class Meta:
        model = Posts
        fields = [
            "id",
            "name",
            "translations",
            "tags",
        ]
@rtpg
Copy link
Contributor

rtpg commented Jan 8, 2023

@mirodil1 do you have a full stack trace for these errors? this is not enough information for us to figure things out.

@mirodil1
Copy link
Author

mirodil1 commented Jan 9, 2023

@rtpg no atm, I guess the problem was with django-parler, it creates translations table and there is also field for django-taggit, in serializer it should be defined as tags=TagListSerializerField() , and for tags field in translations also should be TagListSerializerField() somehow.

@rosamundm
Copy link

Hi, I came across this issue as I got the same error when using tags with factory-boy (otherwise everything is working great, thanks!). Would be great if you could take another look at it.

I added the following to my existing factory class, as suggested in your docs:

@post_generation
def tags(self, create, extracted, **kwargs):
    if not create:
        return
    if extracted:
        self.tags.add(*extracted)

Then when I ran my tests, I got the following stack trace, in case that helps you figure out the problem:

self = <json.encoder.JSONEncoder object at 0x107460820>
o = <taggit.managers._TaggableManager object at 0x109af23a0>

    def default(self, o):
        """Implement this method in a subclass such that it returns
        a serializable object for ``o``, or calls the base implementation
        (to raise a ``TypeError``).
    
        For example, to support arbitrary iterators, you could
        implement default like this::
    
            def default(self, o):
                try:
                    iterable = iter(o)
                except TypeError:
                    pass
                else:
                    return list(iterable)
                # Let the base class default method raise the TypeError
                return JSONEncoder.default(self, o)
    
        """
>       raise TypeError(f'Object of type {o.__class__.__name__} '
                        f'is not JSON serializable')
E       TypeError: Object of type _TaggableManager is not JSON serializable

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py:179: TypeError

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

3 participants