I'm trying to create embedded documents in mongodb using django. I'm facing this error when I tried to insert a post in database.
"ValueError at /post/Value: None must be instance of Model: <class 'django.db.models.base.Model'>"
Here is my code.
from djongo import models
# Schema for comments collection which include in belonging post
class Comments(models.Model):
username = models.CharField(max_length=25, blank=False)
comment = models.TextField()
time_stamp = models.DateTimeField(auto_now_add=True)
# Schema for post collection
class Post(models.Model):
username = models.CharField(max_length=25, blank=False)
title = models.CharField(max_length=100, blank=False)
post = models.TextField()
time_stamp = models.DateTimeField(auto_now_add=True)
comments = models.EmbeddedModelField('Comments')
# allows to use all functionality of django orm
objects = models.DjongoManager()
def __str__(self):
return self.post
serializers.py
from rest_framework import serializers
from models import Post, Comments
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ('id', 'username', 'title', 'post', 'time_stamp')
class CommentsSerializer(serializers.ModelSerializer):
class Meta:
model = Comments
fields = '__all__'
views.py
from rest_framework_mongoengine import generics
from serializers import PostSerializer, CommentsSerializer
from models import Post, Comments
class PostAPIView(generics.CreateAPIView):
lookup_field = 'id'
serializer_class = PostSerializer
def get_queryset(self):
return Post.__objects.all()
class CommentsAPIView(generics.CreateAPIView):
lookup_field = 'id'
serializer_class = CommentsSerializer
def get_queryset(self):
return Comments.__objects.all()
what is the problem with the code? Is this right method to create embedded documents in mongodb?
I used python3.6.4, django2.1, djongo1.2.29 and other libraries like pymongo and mongoengine