简体   繁体   中英

django model with only one CharField is not JSON serializable

I have a very basic 'Place' model:

from django.db import models

class Place(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

and I'm trying to review it using the app: https://github.com/diefenbach/django-reviews

When trying to add a review to certain Place object, in the app's views https://github.com/diefenbach/django-reviews/blob/master/reviews/views.py , it reaches te 'save' method and actually saves a record in the database. However it raises the following TypeError:

Environment:


Request Method: POST
Request URL: http://ec2-54-77-118-44.eu-west-1.compute.amazonaws.com:5000/reviews/add/11/1

Django Version: 1.6.5
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'south',
 'places',
 'reviews')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/ubuntu/webapps/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  199.                 response = middleware_method(request, response)
File "/home/ubuntu/webapps/local/lib/python2.7/site-packages/django/contrib/sessions/middleware.py" in process_response
  38.                     request.session.save()
File "/home/ubuntu/webapps/local/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py" in save
  57.             session_data=self.encode(self._get_session(no_load=must_create)),
File "/home/ubuntu/webapps/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in encode
  87.         serialized = self.serializer().dumps(session_dict)
File "/home/ubuntu/webapps/local/lib/python2.7/site-packages/django/core/signing.py" in dumps
  88.         return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "/usr/lib/python2.7/json/__init__.py" in dumps
  250.         sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py" in encode
  207.         chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py" in iterencode
  270.         return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py" in default
  184.         raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /reviews/add/11/1
Exception Value: <Place: Teteria India> is not JSON serializable

I don't understand why this error raises if I'm still not using any AJAX or JSON and as far as I could see neither does the reviews app's views. Does it have anything to do with the forms?

And anyway, how can it be that a model with only one CharField is not JSON serializable? Do I have to provide the serialization?

Thanks!

In the line 148, it store object in the session

request.session["last-rated-object"] = object

then perfoms a redirect to reviews_thank_you's view

def thank_you(request, template_name="reviews/thank_you.html"):
    """Displays a thank you page.
    """
    if request.session.has_key("last-rated-object"):
        object = request.session.get("last-rated-object")
        del request.session["last-rated-object"]
    else:
        object = None

    return render_to_response(template_name, RequestContext(request, {
        "object" : object,
    }))

It is returning the object without serialize it and i think here is the problem. Can you edit that view to return a serialize version of you object ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM