ValueError at /news/ Value: None must be instance of Model: <class 'django.db.models.base.Model'>

539 views
Skip to first unread message

Sagar

unread,
Aug 19, 2018, 10:02:49 AM8/19/18
to Django users
Hi,
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.
models.py
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

Mikhailo Keda

unread,
Aug 20, 2018, 2:48:17 AM8/20/18
to Django users
Not sure what you are using but all packages for Django + MongoDB are outdated.
Why do not use PostgreSQL instead of MongoDB?

Sagar

unread,
Aug 20, 2018, 7:36:15 AM8/20/18
to Django users
Hi Mikhailo
Yes you are right, but I didn't used django-nonrel or djangotoolbox. Those were totally outdated. I have used djongo which is updated as far as I know. About MongoDB, I'm working on project in which we used MongoDB. We want to use NoSQL database insted of SQL database. Did you work with django and mongodb before?

Jason

unread,
Aug 20, 2018, 7:51:18 AM8/20/18
to Django users
We want to use NoSQL database insted of SQL database

Is your data relational, and are you going to be doing alot of joins?  If so, using mongo will be a pain in your application for performance. 

I really hope you have a solid technical reason why to use mongo rather than "ITS WEBSCALE!!!!!"

Mikhailo Keda

unread,
Aug 20, 2018, 12:48:56 PM8/20/18
to Django users
rest_framework_mongoengine was updated 1 year ago, according to djongo docs they have integration with Django Rest Framework, try to use Django Rest Framework instead of rest_framework_mongoengine.
And you need debug the error.

понеділок, 20 серпня 2018 р. 14:36:15 UTC+3 користувач Sagar написав:

Sagar

unread,
Aug 22, 2018, 2:28:29 AM8/22/18
to Django users
Hi Mikhailo
I think the problem is with views. Because when I run the API, it is giving me the default root view that django provide for API. When I entered the values in form and then
when i post the data, at that time I'm getting this error.
In serializers When I make a small change It is showing me different error like this.
"AssertionError at /post/
No exception message supplied"
here is the code for serializers that I modify
serializers.py
from rest_framework import serializers
from models import Post, Comments


class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = '__all__'

class CommentsSerializer(serializers.ModelSerializer):
class Meta:
model = Comments
fields = '__all__'

I'm new to django. Can you tell me how can I debug the code? Should I use try-catch?

ganesh c

unread,
Oct 1, 2018, 9:21:33 AM10/1/18
to Django users
Hi Sagar,
 I am facing the similar issue while adding User model as embeddedmodel in a custom profile model.
Please let me know do you get any solutions for your issue,

Thanks,
Ganesh

team.a...@gmail.com

unread,
Jan 26, 2019, 9:43:57 AM1/26/19
to Django users
This usually happens when you have added and EmbeddedModel and not passing any value to that field while creation.
Hope this helps.

mahesh boini

unread,
Jan 28, 2019, 7:20:10 AM1/28/19
to django...@googlegroups.com
import proper package you were importing "djongo" it should be "django".

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f0069400-e4e4-41a4-8503-6662a7f624c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages