threeohclock / google-app-engine-django

Automatically exported from code.google.com/p/google-app-engine-django
Apache License 2.0
0 stars 0 forks source link

Django json serializer not compatible with some model property types #52

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create model class which includes unsupported property type (e.g.
UserProperty)
2. Populate and save at least one instance of model class
3. Import serializers from django.core
4. Attempt to serialize with "json"

--Example code--
from django.core import serializers
from google.appengine.ext import db
from google.appengine.api import users
from appengine_django.models import BaseModel

class BadJsonSerialize(BaseModel):
    data = db.StringProperty()
    owner = db.UserProperty()

#user = users.get_current_user()
user = users.User(email='digitalchckn@gmail.com')
inst = BadJsonSerialize(data='A String!', owner=user)
inst.save()

result = serializers.serialize("json", BadJsonSerialize.all())
print result
----------------
What is the expected output? What do you see instead?
Expect to see {"pk":"....", "model":"BadJsonSerialize", "fields":{
  "data":"A String!", "owner": "digitalchckn@gmail.com"}}
or some similar representation for certain 'complex' properties.

Instead, the following error is returned during serialization:
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File
"/usr/local/google_appengine/current/lib/django/django/core/serializers/__init__
.py",
line 67, in serialize
    s.serialize(queryset, **options)
  File
"/usr/local/google_appengine/current/lib/django/django/core/serializers/base.py"
,
line 49, in serialize
    self.end_serialization()
  File
"/usr/local/google_appengine/current/lib/django/django/core/serializers/json.py"
,
line 27, in end_serialization
    simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder,
**self.options)
  File
"/usr/local/google_appengine/current/lib/django/django/utils/simplejson/__init__
.py",
line 138, in dump
    for chunk in iterable:
  File
"/usr/local/google_appengine/current/lib/django/django/utils/simplejson/encoder.
py",
line 262, in _iterencode
    for chunk in self._iterencode_list(o, markers):
  File
"/usr/local/google_appengine/current/lib/django/django/utils/simplejson/encoder.
py",
line 170, in _iterencode_list
    for chunk in self._iterencode(value, markers):
  File
"/usr/local/google_appengine/current/lib/django/django/utils/simplejson/encoder.
py",
line 265, in _iterencode
    for chunk in self._iterencode_dict(o, markers):
  File
"/usr/local/google_appengine/current/lib/django/django/utils/simplejson/encoder.
py",
line 235, in _iterencode_dict
    for chunk in self._iterencode(value, markers):
  File
"/usr/local/google_appengine/current/lib/django/django/utils/simplejson/encoder.
py",
line 265, in _iterencode
    for chunk in self._iterencode_dict(o, markers):
  File
"/usr/local/google_appengine/current/lib/django/django/utils/simplejson/encoder.
py",
line 235, in _iterencode_dict
    for chunk in self._iterencode(value, markers):
  File
"/usr/local/google_appengine/current/lib/django/django/utils/simplejson/encoder.
py",
line 273, in _iterencode
    for chunk in self._iterencode_default(o, markers):
  File
"/usr/local/google_appengine/current/lib/django/django/utils/simplejson/encoder.
py",
line 279, in _iterencode_default
    newobj = self.default(o)
  File
"/usr/local/google_appengine/current/lib/django/django/core/serializers/json.py"
,
line 62, in default
    return super(DjangoJSONEncoder, self).default(o)
  File
"/usr/local/google_appengine/current/lib/django/django/utils/simplejson/encoder.
py",
line 300, in default
    raise TypeError("%r is not JSON serializable" % (o,))
TypeError: users.User(email='digitalchckn@gmail.com') is not JSON serializable

What version of the product are you using? On what operating system?
App Engine SDK v1.1.0.1 - Mac OS X 10.5.3

Please provide any additional information below.

I suspect this issue occurs for any of the following properties, though I
don't have the opportunity to test them individually.  I also suspect the
deserialization of these objects fails too.

db.ReferenceProperty
db.BlobProperty (may not have a desirable json representation)
db.CategoryProperty
db.LinkProperty
db.EmailProperty
db.GeoPtProperty
db.IMProperty
db.PhoneNumberProperty
db.PostalAddressProperty
db.RatingProperty

db.UserProperty ... since a User object does not inherit from Model, it
suffers from other serialization problems when the serializer attempts to
read its _meta.fields property.

Original issue reported on code.google.com by zenchic...@live.com on 20 Jun 2008 at 5:13

Attachments:

GoogleCodeExporter commented 8 years ago

Original comment by mattbrow...@gmail.com on 24 Jun 2008 at 10:59

GoogleCodeExporter commented 8 years ago
Indeed, 
JSON serialization seems to be not working for GAE models.

Original comment by hus...@gmail.com on 10 Jul 2008 at 4:57

GoogleCodeExporter commented 8 years ago
was this fixed? 

Original comment by kfitzger...@gmail.com on 9 Oct 2008 at 3:55

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Still broken for me.
Here is my model:

class Page(db.Model):
    adventure = db.ReferenceProperty(Adventure)
    name = db.StringProperty()

Original comment by tst...@gmail.com on 5 Dec 2008 at 10:21

GoogleCodeExporter commented 8 years ago
Purpose of code changes on this branch:

This patch fixes serialization for some ReferenceProperty items, and properly
serializes ListProperty items containing item keys.

After the review, I'll merge this branch into:
/trunk

Original comment by datavor...@gmail.com on 15 May 2009 at 9:10

Attachments:

GoogleCodeExporter commented 8 years ago
When try to do JSON serialize like this: 
  def get(self):
     #query = db.GqlQuery("SELECT * FROM Subscriber WHERE serviceCode = :1",
"oneCloudI") 
     query = db.GqlQuery("SELECT * FROM Subscriber") 
     LIMIT = 1000
     subscriberList = query.fetch(LIMIT,offset=0);
     json = serializers.serialize("json", subscriberList) 
     return HttpResponse(json, mimetype="application/json") 

I'm getting this error: 
Traceback (most recent call last):
  File "c:\Program
Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 
498, in
__call__
    handler.get(*groups)
  File "d:\GoogleAppEngine\dbpolymorph\demo.py", line 334, in get
    json = serializers.serialize("json", subscriberList)
  File "c:\Program
Files\Google\google_appengine\lib\django\django\core\serializers\__init__.py", 
line
67, in serialize
    s.serialize(queryset, **options)
  File "c:\Program
Files\Google\google_appengine\lib\django\django\core\serializers\base.py", line 
36,
in serialize
    for field in obj._meta.fields:
AttributeError: 'Subscriber' object has no attribute '_meta'

Original comment by nealwalt...@gtempaccount.com on 21 Jun 2009 at 4:15