Why doesn't this work?

Skip to first unread message

John Dungan

Apr 15, 2009, 11:49:33 PM4/15/09
to Tulsa Python
I'm try to pass a choice from a posted form to this generic django
view. The idea is to select the form rather than create a separate
generic request for each type.

def add_foo(request):
foo_type= request.POST.get('foo_type')
logging.info("FOO TYPE: %s", foo_type)
return create_object(request, form_class=foo_type,

The problem is that is fails on the logging stmt with:

Exception Value: 'unicode' object has no attribute '_meta'

I know this is going to be something simple, so my apologies. Also I
know the next line is trouble so any suggestions on passing the
correct form_type to create_object would be appreciated.

A latte on me at the next meeting to whoever solves this headache.

Devin Venable

Apr 16, 2009, 10:19:18 AM4/16/09
to John Dungan, Tulsa Python

Logging seems to handle both regular and unicode strings.  No exceptions here using Python 2.5.2.

>>> foo_type = u'This is a unicode string'
>>> print type(foo_type)
<type 'unicode'>
>>> import logging
>>> logging.info("foo type: %s", foo_type)

Are you sure the exception is on the logging line? 

Also, which version of Python and Django are you using?  Or is this AppEngine behavior?


Steven Osborn

Apr 16, 2009, 11:30:11 AM4/16/09
to tulsa...@googlegroups.com
Sorry seems I sent this reply to John and not the group.

---------- Forwarded message ----------
From: Steven Osborn <osborn...@gmail.com>
Date: Wed, Apr 15, 2009 at 11:27 PM
Subject: Re: [tulsapython] Why doesn't this work?
To: John Dungan <johna...@gmail.com>

From Django Docs: >>>

If you provide form_class, it should be a django.forms.ModelForm
subclass. Use this argument when you need to customize the model's
form. See the ModelForm docs for more information.

Otherwise, model should be a Django model class and the form used will
be a standard ModelForm for model.


objects of ModelForm or Model type will have a _meta attribute.

I would use an if - else to determine what type of object your creating like:

#start code
from myapp.models import Foo, Bar

if "foo" in request.POST.get('foo_type'):
  foo_type  = Foo;  # I don't think Django is expecting an instance
here but if so you'll want to do Foo()
else "bar" in request.POST.get('foo_type'):
 foo_type = Bar;


You could use eval() to turn the string into an object but that is a
terrible idea if your app is ever going to be used by anyone besides

Hope this helps.

Steven Osborn
Steven Osborn

Steven Osborn

Apr 16, 2009, 11:30:35 AM4/16/09
to tulsa...@googlegroups.com
And John replied to me. :-P

---------- Forwarded message ----------
From: gmail <johna...@gmail.com>
Date: Thu, Apr 16, 2009 at 4:21 AM
Subject: Re: [tulsapython] Why doesn't this work?
To: Steven Osborn <osborn...@gmail.com>

Works like a charm.

You were right about my plan to use a string, so I've skipped it.

Thanks for your help.

John Dungan

Apr 16, 2009, 1:17:17 PM4/16/09
to Tulsa Python
Steve's suggestion helped me move on, but I do need to point out that
for some reason the str and unicode would not work in the logging
stmt. I know it should, but it didn't. In the 'real' code I put a
simple log stmt right before this one and it worked and blew up on the
line in question. I have no doubt that the create_object would have
been a non-starter for the same reason, but it never got that far.
Reply all
Reply to author
0 new messages