I have these models:
class Projects(models.Model):
projectName =models.CharField(max_length = 100,unique=True,db_index=True)
projectManager = EmbeddedModelField('Users')
class Users(models.Model):
name = models.CharField(max_length = 100,unique=True)
designation = models.CharField(max_length =100 )
I need to return JSON from my view for all Projects objects,I tried json.dumps(Projects.objects.all()) but it did not worked,how do i accomplished it??
EDIT:
I used json.dumps(Projects.objects.all().values()) and got this:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "D:\Python27\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "D:\Python27\lib\json\encoder.py", line 203, in encode
chunks = list(chunks)
File "D:\Python27\lib\json\encoder.py", line 436, in _iterencode
o = _default(o)
File "D:\Python27\lib\json\encoder.py", line 178, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: [{'projectName': u'HELLO', 'projectManager': {u'id': u'4eb3b792b990a24e49f6bb26', u'name': u'anshul', u'designation': u'programmer', u'teams': []}, '
id': u'4eb3b7d0e814520db4000000'}] is not JSON serializable
Now i am getting the result what i want but why is this giving me error as well.
Querysets are not serializable. You could try to use list(self.objects) instead self.objects to force the queryset to be processed as a list. In your case try something like this