Model object has no attribute 'get'

4,205 views
Skip to first unread message

phoebebright

unread,
Sep 21, 2009, 10:05:16 AM9/21/09
to Django users
Have been stuck on this one for a couple of days so hoping for some
enlightenment from some more able django users. Am aware of the
'gotcha' where the model name matches and attribute or one of the Meta
values.

I am getting this when a form is instantiated with an existing object
- no problem if the form is blank.
Have tried renaming the model and renaming most of the fields in case
the model name was used elsewhere or the fields were reserved words.
Not sure what else to try and suspect the real problem is that I don't
understand what the error message is trying to tell me!

Here is the model:

class TweetLog(models.Model):

tweet = models.TextField()

content_type = models.ForeignKey(ContentType, blank=True,
null=True)
object_id = models.PositiveIntegerField(blank=True, null=True)
content_object = generic.GenericForeignKey('content_type',
'object_id')

project = models.ForeignKey(Project, blank=True, null=True)
attached_notes = models.TextField(blank=True, null=True)
attached_url = models.URLField(blank=True, null=True)
attached_file = models.FileField(upload_to="uploads/files/",
blank=True, null=True)
attached_image = models.FileField(upload_to="uploads/images/",
blank=True, null=True)

owner = models.ForeignKey(Who, related_name='tweeter')
created = models.DateTimeField(default=datetime.now)

And the traceback - it gets to the template before complaining:

Original Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/template/debug.py", line 71, in
render_node
result = node.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/template/debug.py", line 87, in render
output = force_unicode(self.filter_expression.resolve(context))
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/utils/encoding.py", line 71, in
force_unicode
s = unicode(s)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/forms/forms.py", line 356, in
__unicode__
return self.as_widget()
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/forms/forms.py", line 386, in as_widget
data = self.data
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/forms/forms.py", line 413, in _data
return self.field.widget.value_from_datadict(self.form.data,
self.form.files, self.html_name)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/forms/widgets.py", line 170, in
value_from_datadict
return data.get(name, None)
AttributeError: 'TweetLog' object has no attribute 'get'

Karen Tracey

unread,
Sep 21, 2009, 10:25:47 AM9/21/09
to django...@googlegroups.com
On Mon, Sep 21, 2009 at 10:05 AM, phoebebright <phoebe...@spamcop.net> wrote:

Have been stuck on this one for a couple of days so hoping for some
enlightenment from some more able django users.  Am aware of the
'gotcha' where the model name matches and attribute or one of the Meta
values.

I am getting this when a form is instantiated with an existing object
- no problem if the form is blank.


Since you are getting an error when the form is instantiated with an existing object, that would have been more helpful code to post than the model definition.  It appears based on the error you are getting that in this case you are passing the existing object as the first positional argument?  At any rate the model instance you are using appears to be getting assigned to the form data attribute, which could happen if you are passing the instance as the first positional argument when you create the form. 

I do not think the problem has anything to do with the names of the fields in your model but rather it has to do with exactly what code you are using to create the form when you have an existing instance you want to use.

Karen

phoebebright

unread,
Sep 21, 2009, 11:49:38 AM9/21/09
to Django users

On Sep 21, 3:25 pm, Karen Tracey <kmtra...@gmail.com> wrote:
Karen,

Spot on with the first positional argument I think, but I now get the
error:
form = TweetForm(instance=obj)
TypeError: __init__() got an unexpected keyword argument 'instance'


Here is the view:

if request.method == 'POST':
form = TweetForm(request.POST)
etc.
elif isinstance(obj, TweetLog):
form = TweetForm(instance=obj) <----- had TweetForm(obj)
before!
else:
default_taglist = request.session.get('taglist', '')
form = TweetForm(initial = {'tweet_taglist' :
default_taglist}) # An unbound form

return form

And the form:

class TweetForm(forms.Form):

tweet = forms.CharField(widget=TweetFieldWidget(), required=True)
tweet_taglist = forms.CharField(max_length=100, required=False)
process_backwards = forms.BooleanField(required=False)

attached_notes = forms.CharField(widget=forms.Textarea,
required=False)
attached_url = forms.URLField(required=False)
attached_file = forms.FileField(required=False)
attached_image = forms.ImageField(required=False)

class Media:
js = ('/js/jquery.js', '/js/jquery-ui.min.js','js/
jeditable.js','js/tweetform.js', 'js/jquery-ui.tabs.js')
css = { 'all': ('/css/jquery-ui.css','/css/smoothness/jquery-
ui-1.7.2.custom.css') }

class Meta :
model = TweetLog
fields =
('tweet','attached_notes','attached_url','attached_file','attached_image')

Karen Tracey

unread,
Sep 21, 2009, 11:54:43 AM9/21/09
to django...@googlegroups.com
On Mon, Sep 21, 2009 at 11:49 AM, phoebebright <phoebe...@spamcop.net> wrote:

On Sep 21, 3:25 pm, Karen Tracey <kmtra...@gmail.com> wrote:
Karen,

Spot on with the first positional argument I think, but I now get the
error:
form = TweetForm(instance=obj)
TypeError: __init__() got an unexpected keyword argument 'instance'


Here is the view:

   if request.method == 'POST':
       form = TweetForm(request.POST)
       etc.
   elif isinstance(obj, TweetLog):
       form = TweetForm(instance=obj)     <----- had TweetForm(obj)
before!
   else:
       default_taglist = request.session.get('taglist', '')
       form = TweetForm(initial = {'tweet_taglist' :
default_taglist}) # An unbound form

   return form

And the form:

class TweetForm(forms.Form):


TweetForm isn't a ModelForm, therefore it doesn't support passing a model instance as a parameter.  Given it looks like you are giving it a Meta class as if it were a ModelFrom below, perhaps all you need to do to fix it is change the base class here.
 

phoebebright

unread,
Sep 21, 2009, 3:16:52 PM9/21/09
to Django users
Karen,

Yes absolutely right. Thanks for rescuing me from own stupidity!

On Sep 21, 4:54 pm, Karen Tracey <kmtra...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages