ManyRelatedField support
onebit0fme opened this issue · 2 comments
onebit0fme commented
Thanks for the package, although I've spent quite some time to make it work with ManyRelatedField, even though the solution was quite simple.
The reason I use ManyRelatedField is because ListField raises 'RelatedManager' is not iterable
, which makes sense.
My solution is:
class RecursiveManyRelatedField(serializers.ManyRelatedField, serializers.BaseSerializer):
pass
class CommentSerializer(serializers.ModelSerializer):
children = RecursiveManyRelatedField(child_relation=RecursiveField(to='CommentSerializer', allow_null=True), read_only=True)
to avoid:
assert issubclass(parent_class, BaseSerializer) #line 83 of fileds.py
Works like a charm, just thought it should be supported. Thank you.
heywbj commented
What version of django-rest-framework are you using?
could you give me a snippet of what your model looks like?
onebit0fme commented
False alarm
I forgot that I can set many=True
, thus fixing my issue. Duh. Instead, I 'hacked' it with the following:
serializers.py
class RecursiveManyRelatedField(serializers.ManyRelatedField, serializers.BaseSerializer):
"""
Workaround to avoid "assert issubclass(parent_class, BaseSerializer)"
"""
pass
class NodeCommentSerializer(serializers.ModelSerializer):
parent = serializers.PrimaryKeyRelatedField(queryset=NodeComment.objects.all(), allow_null=True)
children = RecursiveManyRelatedField(child_relation=RecursiveField(to='NodeCommentSerializer', allow_null=True), read_only=True)
# This is how it supposed to be:
# children = RecursiveField(to='NodeCommentSerializer', many=True, read_only=True)
class Meta:
model = NodeComment
models.py
from django_comments.models import Comment, CommentManager
class NodeComment(Comment):
title = models.TextField(_('Title'), blank=True)
parent = models.ForeignKey('self', null=True, blank=True, default=None, related_name='children', verbose_name=_('Parent'))
objects = CommentManager()
Anyway, thanks again for the package, you can close the issue.