1

I am trying to make a comment system that uses MongoDB as the backend and Django Rest Framework and Mongoengine as the front end.

The comment system would save Comments in the database and every comment will have one and only one Author.

The following approach works for me but I wanted to ask you if this is the advisable way to go about it.

I have a Comment document that embeds the Author document.

class Author(EmbeddedDocument):
    id = StringField(required=True,primary_key=True)
    author_name = StringField()

class Comment(Document):
    discussion_id = StringField(required=True)
    parent_id = ObjectIdField(required=False)
    slug = StringField()
    full_slug= StringField()
    posted = DateTimeField()
    text = StringField()
    author = EmbeddedDocumentField(Author)

    meta = {
        'indexes': [ {'fields' : ['parent_id', 'id'] }]
    }

This is how I have defined the serialisers:

I have defined two serialisers, CommentSerialiser

class CommentSerialiser(serializers.Serializer):
    id = serializers.CharField(required=True,max_length=50)
    discussion_id = serializers.CharField()
    parent_id = serializers.CharField()
    slug = serializers.CharField()
    full_slug= serializers.CharField()
    posted = serializers.DateTimeField()
    text = serializers.CharField()
    author = AuthorSerialiser(required=False)

    def restore_object(self, attrs, instance=None):
        if instance:
            instance.id = attrs.get('id', instance.id)
            instance.discussion_id = attrs.get('discussion_id', instance.discussion_id)
            instance.parent_id = attrs.get('parent_id', instance.parent_id)
            instance.slug = attrs.get('slug', instance.slug)
            instance.full_slug = attrs.get('full_slug', instance.full_slug)
            instance.posted = attrs.get('posted', instance.posted)
            instance.text = attrs.get('text', instance.text)
            if instance.author is None:
               instance.author = attrs.get('author', instance.author)                                                                          
            return instance
        return Comment(**attrs)

and AuthorSerialiser which has overridden to_native and field_to_native methods:

class AuthorSerialiser(serializers.Serializer):
    id = serializers.CharField(required=True,max_length=50)
    author_name = serializers.CharField(required=True,max_length=50)

    def to_native(self, obj):

        ret = self._dict_class()
        ret.fields = self._dict_class()

        ret.fields["id"]="id"
        ret["id"]=obj.id
        ret.fields["name"]="name"
        ret["name"]=obj.author_name


        return ret

    def field_to_native(self, obj, field_name):

        return self.to_native(obj.author)

1 Answers1

0

You don't need to define to_native and field_to_native in AuthorSerialiser. Also in CommentSerialiser restore_object you put if instance.author is None: and i think should be if instance.author is not None: but even that could be erased; you could put instance.author = attrs.get('author', instance.author) without the if.

One more thing if Author is EmbeddedDocument shouldn't need id, if its needs id you should declare it as a Document and the relation should be a ReferenceField.