brian-zhao / 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

Unable to render form with non-empty ReferenceProperty #179

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. follow the guide on http://www.deck.cc/django_1.2_on_google_app_engine.html
2. create 2 simple models with db.ReferenceProperty on one of them:
from appengine_django.models import BaseModel
from google.appengine.ext import db

class Category(BaseModel):
    name = db.CategoryProperty(required=True)    
    date = db.DateTimeProperty(required=True, auto_now_add=True)            
    def __unicode__(self):
        return self.name        

class Subcategory(BaseModel):
    name = db.CategoryProperty(required=True)
    category = db.ReferenceProperty(Category, required=True)    
    date = db.DateTimeProperty(required=True, auto_now_add=True)
    def __unicode__(self):
        return self.name

3. try to render the Subcategory modelform with (empty at this point) 
db.ReferenceProperty field (which works)

4. add one record to Category model, so that field would contain some choice 
and try to render the Subcategory form again (which fails here)

What is the expected output? What do you see instead?
The rendered form with some choice values is expected. I get this error 
pointing to template line with form rendering call (be it "{{ form.as_p }}" or 
"{% for f in form %} {{ field }}…"):

Caught BadValueError while rendering: tag should be a string; received 
Category(**{'date': datetime.datetime(2010, 7, 23, 8, 16, 23, 166011), 'name': 
u'cat1'}) (a Category)

What version of the product are you using? On what operating system?
helper ~ r105, django 1.2.1, google_appengine 1.3.5, python 2.5.5, linux 2.6

Maybe it has sonething to do with the way django forms and GAE models are 
mixed? Is there another simple way doing the same basic hierarchy things?

The relevant files are attached.

Original issue reported on code.google.com by fake.mik...@gmail.com on 10 Aug 2010 at 10:37

Attachments:

GoogleCodeExporter commented 8 years ago
I was running into the same issue.  I eventually got it to work after much 
trial and error.  It seems the problem was that I had a __unicode__ function 
defined for my model that had a try...except clause in it.  Don't ask me why 
that would cause a problem, but it seems it made the unicode conversion fail 
(in the interactive shell it reported that it couldn't convert NoneType to 
unicode).  All I know is that once I changed the logic in the __unicode__ 
function to if...else instead of try...except, it worked.  Hope this helps 
someone who runs into the same issue.

Original comment by xorbi...@gmail.com on 17 Sep 2010 at 5:11

GoogleCodeExporter commented 8 years ago
Thanks, in my case it was really related to the __unicode__() method of the 
model.
def __unicode__(self):
    # works:
    return u'%s' % self.name
    # fails with the described above error:
    #return self.name

This is odd as it works in plain django. Can it be considered a bug or a place 
for an improvement? If not -- feel free to close the issue.

Original comment by fake.mik...@gmail.com on 18 Nov 2010 at 9:48