Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

GenericRelation Support #224

Closed
Closed
@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