umutbozkurt/django-rest-framework-mongoengine

Upon deserializing, reference fields are not present within the validated_data

ris-tlp opened this issue · 1 comments

Models being used (issue lies only with the last one - ForumPost, but adding the rest of the models for completeness):

class Tag(Document):
    title = fields.StringField(required=True)
    icon = fields.StringField(required=True)

    meta = {"collection": "Tag"}

class Subforum(Document):
    title = fields.StringField(required=True)
    description = fields.StringField(required=True)
    icon = fields.StringField(required=True)
    total_posts = fields.IntField()

    meta = {"collection": "Subforum"}

class User(Document):
    username = fields.StringField(required=True, max_length=15)
    password = fields.StringField(required=True)
    profile_picture = fields.StringField()
    travel_points = fields.IntField()
    user_type = fields.StringField(required=True, default="tourist")

    meta = {"collection": "User"}

class ForumPost(Document):
    title = fields.StringField(required=True)
    content = fields.StringField(required=True)
    author = fields.ReferenceField(User, required=True)
    likes = fields.IntField()
    tags = fields.ListField(fields.ReferenceField(Tag))
    subforum = fields.ReferenceField(Subforum)
    parent_post = fields.ReferenceField("self")

    meta = {"collection": "ForumPost"}

Serializer for ForumPost:

class ForumPostSerializer(serializers.DocumentSerializer):
    class Meta:
        model = ForumPost
        depth = 2
        fields = ["id", "title", "content", "likes", "author", "tags", "parent_post", "subforum"]

Issue

Upon serializing a document that's been fetched from MongoDB, everything is fine:

post_data = ForumPost.objects(title="forum-post-title-test-new").get()
serialized_data = ForumPostSerializer(post_data).data

# Simulating a render to json -> send over http request -> parsing json stream 
json = JSONRenderer().render(serialized_data)
stream = io.BytesIO(json)
data = JSONParser().parse(stream)

print(data)
print(type(data))

Output

{'id': '630e45ca6d0f878155feee48', 'title': 'forum-post-title-test-new', 'content': 'forum-post-title-test-content', 'likes': 0, 'author': {'id': '630a3643c6668ca6aa7bb8d2', 'username': 'omar', 'password': '$2a$12$w1LM8ZUk7UHvfBiioUTtJeNYrZklRQ/sBY/aPmUT/rFMAPOjG.Iyu', 'profile_picture': 'lmao.jpg', 'travel_points': 1500, 'user_type': 'tourist'}, 'tags': [{'id': '62f73215e0bed4f3d0a0bc8e', 'title': 'tag-test', 'icon': 'tag-icon-test'}], 'parent_post': None, 'subforum': {'id': '630a91b330eb5eb6acbe34f5', 'title': 'subforum-test', 'description': 'subforum-description', 'icon': 'icon.png', 'total_posts': 5}}

As you can see, all the fields exist in correspondence to the ForumPost model. (The parent_post is null in the document so nothing to note there).

On deserializing this same object:

serializer = ForumPostSerializer(data=data)
print(f"Serializer valid: {serializer.is_valid()}")
print(serializer.validated_data)

I get the output:

Serializer valid: True
OrderedDict([('title', 'forum-post-title-test-new'), ('content', 'forum-post-title-test-content'), ('likes', 0)])

All the reference fields are missing within the deserialized object even though the it shows as valid. Am I doing something wrong here? Any help would be appreciated!

IATF commented