'QuerySet' object has no attribute '_meta'

5,581 views
Skip to first unread message

ayayalar

unread,
Nov 21, 2008, 4:22:00 PM11/21/08
to Django users
I am just simply trying to display all existing objects in a form

MODEL:
class Product(models.Model):
name = models.CharField(max_length=30)

def __unicode__(self):
return self.name

VIEW
def product(request):
product = Product.objects.all()
form = ProductForm(instance=product)
return render_to_response('index.html', {'form' : form})


Throws this error:
=============

Environment:

Request Method: GET
Request URL: http://localhost:8000/product/
Django Version: 1.0.1 final
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'demo.home']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "C:\Python25\lib\site-packages\django\core\handlers\base.py" in
get_response
86. response = callback(request, *callback_args,
**callback_kwargs)
File "C:\Django\demo\..\demo\home\views.py" in product
10. form = ProductForm(instance=product)
File "C:\Python25\Lib\site-packages\django\forms\models.py" in
__init__
216. object_data = model_to_dict(instance, opts.fields,
opts.exclude)
File "C:\Python25\Lib\site-packages\django\forms\models.py" in
model_to_dict
119. opts = instance._meta

Exception Type: AttributeError at /product/
Exception Value: 'QuerySet' object has no attribute '_meta'




Any suggestions?

Karen Tracey

unread,
Nov 21, 2008, 6:00:02 PM11/21/08
to django...@googlegroups.com

You don't show us ProductForm but I can guess it is a ModelForm for Product.  A ModelForm is designed to display and let you edit one individual object from the DB, not multiple.  Thus the 'instance' parameter to a model form is supposed to be one single instance of a model, not a QuerySet.  A model formset might be closer to what you are looking for:

http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#id1

Karen

ayayalar

unread,
Nov 21, 2008, 6:23:31 PM11/21/08
to Django users
Thanks Karen. That's exactly the case. Link you provided very helpful.

On Nov 21, 3:00 pm, "Karen Tracey" <kmtra...@gmail.com> wrote:

ayayalar

unread,
Nov 21, 2008, 7:25:46 PM11/21/08
to Django users
Karen,

On this note, is it possible to customize the widget's for formsets as
well?


Thank you.

alex....@gmail.com

unread,
Nov 21, 2008, 8:10:18 PM11/21/08
to Django users
You can pass modelformset_factory a kwarg form which should just me
the form class you want to use(aka make a ModelForm with the changes
to the widgets).

Alex
Reply all
Reply to author
Forward
0 new messages