type object 'MyModel'' has no attribute 'id'

1,161 views
Skip to first unread message

Carlos Eduardo Sotelo Pinto

unread,
Jul 4, 2009, 3:42:52 PM7/4/09
to django...@googlegroups.com
Hi people... I have this traceback error and i am something lost, thanks

Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/support/helping/product/1/1
Django Version: 1.0.2 final
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.webdesign',
'jaamsa.app.support']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "/var/lib/python-support/python2.5/django/core/handlers/base.py"
in get_response
86. response = callback(request, *callback_args,
**callback_kwargs)
File "/mnt/devel/djprojects/jaamsa/../jaamsa/app/support/views.py" in product
52. pmform = ProductModelForm(maker_filter=maker_id,
data=None, instance=ProductModel)
File "/mnt/devel/djprojects/jaamsa/../jaamsa/app/support/forms.py" in __init__
54. super(ProductModelForm, self).__init__(*args, **kwargs)
File "/var/lib/python-support/python2.5/django/forms/models.py" in __init__
216. object_data = model_to_dict(instance, opts.fields,
opts.exclude)
File "/var/lib/python-support/python2.5/django/forms/models.py" in model_to_dict
138. data[f.name] = f.value_from_object(instance)
File "/var/lib/python-support/python2.5/django/db/models/fields/__init__.py"
in value_from_object
332. return getattr(obj, self.attname)

Exception Type: AttributeError at /support/helping/product/1/1
Exception Value: type object 'ProductModel' has no attribute 'id'

#----model
class ProductModel (models.Model):
maker = models.ForeignKey(Maker)
product_type = models.ForeignKey(ProductType)
name = models.CharField(u'Modelo', max_length=32)
created = models.DateTimeField(u'Creado', auto_now_add=True)
modified = models.DateTimeField(u'Modificado', auto_now=True)
def __unicode__(self):
return self.name

#---- form
class ProductModelForm(forms.ModelForm):
product_model = forms.ModelChoiceField(ProductModel.objects.all(),
None, u'Modelo')
class Meta:
model = ProductModel
exclude = ['maker', 'product_type']
def __init__(self, maker_filter, *args, **kwargs):
self.base_fields['product_model'].query =
ProductModel.objects.all().filter(maker=maker_filter)
self.base_fields['product_model'].widget.choices =
self.base_fields['product_model'].choices
super(ProductModelForm, self).__init__(*args, **kwargs)

#---- view
def product(request, maker_id, product_type_id):
if request.method == 'POST':
pmform = ProductModelForm(request.POST)
if mform.is_valid():
maker = topic = mform.cleaned_data['maker']
path = '/support/helping/product/%s' % maker
return HttpResponseRedirect(path)
else:
pmform = ProductModelForm(maker_filter=maker_id, data=None,
instance=ProductModel)
return render_to_response('helping.html', {'mform': pmform})


--
Carlos Eduardo Sotelo Pinto a.k.a. krlos
GNULinux RU #379182 || GNULinux RM #277661
GNULinux Arequipa Users Group || Debian Arequipa Users Group
--
http://krlosaqp.blogspot.com
pgp.rediris.es 0xF8554F6B
GPG FP:697E FAB8 8E83 1D60 BBFB 2264 9E3D 5761 F855 4F6B

Alex Gaynor

unread,
Jul 4, 2009, 3:45:04 PM7/4/09
to django...@googlegroups.com
You're passing the class MyModel, instead of either an instance of MyModel (MyModel()) or None.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Daniel Roseman

unread,
Jul 4, 2009, 4:00:40 PM7/4/09
to Django users
On Jul 4, 8:42 pm, Carlos Eduardo Sotelo Pinto
<csot...@aqpglug.org.pe> wrote:
> Hi people... I have this traceback error and i am something lost, thanks
>
<snip>
Wow, quite a lot wrong here, I don't know where to start.

The immediate cause of your problem is that you're passing in the
model class, ProductModel, as the instance parameter. As the name
implies, the instance should be an actual instance - ie an actual
productmodel, not the class.

When you've fixed that, you'll quickly come into problems when you
submit the form. For a start, you don't save it at all; after that,
this line is bound to fail:
maker = topic = mform.cleaned_data['maker']
because you've explicitly *excluded* 'maker' from the list of form
fields.

And then you've got the fact that you've added a product_model field,
where you choose from a list of product models, to the form where you
actually define a product model... that makes no sense. And so on.

My advice is to go back to basics. Read the documentation and make
sure you understand how to define a standard model form. Ensure that
you have the correct logic to instantiate and save a form for both a
completely new item, and for editing an existing one. Then you can try
customising things, a little at a time.

If there are specific things you don't understand, post here and we'll
help. But there's really no point in jumping right in and trying a
whole load of stuff when you haven't got the basics down.
--
DR.
Reply all
Reply to author
Forward
0 new messages