Skip to content

GenericRelation Support #224

@snewcomer

Description

@snewcomer

Is GenericRelation support possible? This is how I would do it with Django rest. Can't get this to work however as I am getting the is not JSON serializable error. Guessing there might be some serialization issue with json api?

# Serializer
class FooSerializer(serializers.ModelSerializer):

    included_serializers = {
        'news': NewsSerializer,
    }

    class Meta:
        model = Foo
        fields = ('id', 'news')


# News Serializer
class NewsSerializer(serializers.ModelSerializer):

    class Meta:
        model = News
        fields = ('id', 'description', 'date')


# Foo Model
class FooModel(models.Model):
    news = GenericRelation(News)


# News Model
class News(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    description = models.TextField(max_length=200, blank=True)
    date = models.DateTimeField(blank=True, null=True)
    content_type = models.ForeignKey(ContentType)
    object_id = models.UUIDField()
    content_object = GenericForeignKey('content_type', 'object_id')

    class JSONAPIMeta:
        resource_name = 'news'

I'd be happy to add some "documentation" through the examples and work through it that way. That is of course if somebody has not already come across this!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy