I have this model to add new products to a database:

--
class Product(models.Model):
        user = models.ForeignKey(User)
        productid = models.CharField(max_length=40, blank=True)
        prodname = models.CharField(max_length=255, verbose_name='Product
name')
        proddesc = models.TextField(blank=True, verbose_name='Description')
        prodtype = models.ForeignKey(Prodtype, verbose_name='Product Type')
        unit = models.ForeignKey(Units, verbose_name='Unit')
        image = models.ManyToManyField('Image', blank=True,
verbose_name='Image')
        video = models.ManyToManyField('Video', blank=True,
verbose_name='Video')
        costperunit = models.DecimalField(max_digits=6, decimal_places=2,
verbose_name='Cost per unit')
        costperlot = models.DecimalField(max_digits=6, decimal_places=2,
verbose_name='Cost per lot')
        available = models.BooleanField(default=True)
        featured = models.BooleanField(blank=True, null=True)
        def __unicode__(self):
                return self.prodname
--
It has a ModelForm with a custom __init__ definition to get an
associated list of video files:

--
class ProductForm(ModelForm):
        def __init__(self, user, *args, **kwargs):
                super(ProductForm, self).__init__(*args, **kwargs)
                self.fields['video'].queryset = Video.objects.filter(user=user)

        class Meta:
                model = Product
                exclude = ('user', 'productid', 'available', 'featured')
--

If I use this model and model form to edit a product using this view,
which I know is a bit hacky, but will be tidied up eventually:

--
def edit_product(request, prodid):
        if request.user.is_authenticated():
                user = request.user
                try:
                        product = Product.objects.get(id = prodid)
                except Product.DoesNotExist:
                        pass
                if request.method == 'POST':
                        productform = ProductForm(request.POST, 
instance=product)
                        productform.user = user
                        if productform.is_valid:
                                productid = productform.save()
                                return 
HttpResponseRedirect('/product/'+productid )
                        else:
                                return 
render_to_response('products/base_newproduct.html',
{ 'productform': productform })
                else:
                        productform = ProductForm(request.user or None, 
instance=product)
                        errormsg = ''
        return render_to_response('products/base_newproduct.html',
{ 'productform': productform })

--

On submitting the query, I get and AttributeError: 'ProductForm'
object has no attribute 'cleaned_data'

I understand why this is returned as I'm not handling the is_valid
statement. However, there are no errors coming back that I can see,
either in the 500 response or if I run the query from a shell. From
the traceback I get this:

--
Traceback:
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py"
in get_response
  86.                 response = callback(request, *callback_args,
**callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/auth/
decorators.py" in __call__
  67.             return self.view_func(request, *args, **kwargs)
File "/var/www/stocktube/products/views.py" in edit_product
  49.                           productid = productform.save()
File "/usr/lib/python2.5/site-packages/django/forms/models.py" in save
  319.         return save_instance(self, self.instance,
self._meta.fields, fail_message, commit)
File "/usr/lib/python2.5/site-packages/django/forms/models.py" in
save_instance
  43.     cleaned_data = form.cleaned_data

Exception Type: AttributeError at /products/edit/1/
Exception Value: 'ProductForm' object has no attribute 'cleaned_data'

--
and the response from the cleaned_data request (doesn't format
properly but read alternate lines)

commit
True
exclude
None
fail_message
'changed'
fields
None
form
<stocktube.products.models.ProductForm object at 0xa08476c>
instance
<Product: 200 iDogs>
models
<module 'django.db.models' from '/usr/lib/python2.5/site-packages/
django/db/models/__init__.pyc'>
opts
<Options for Product>

>From the shell I get this:


--
<type 'exceptions.AttributeError'>        Traceback (most recent call
last)

/home/simong/projects/stocktube/<ipython console> in <module>()

/usr/lib/python2.5/site-packages/django/forms/models.py in save(self,
commit)
    317         else:
    318             fail_message = 'changed'
--> 319         return save_instance(self, self.instance,
self._meta.fields, fail_message, commit)
    320
    321 class ModelForm(BaseModelForm):

/usr/lib/python2.5/site-packages/django/forms/models.py in
save_instance(form, instance, fields, fail_message, commit, exclude)
     41         raise ValueError("The %s could not be %s because the
data didn't"
     42                          " validate." % (opts.object_name,
fail_message))
---> 43     cleaned_data = form.cleaned_data
     44     file_field_list = []
     45     for f in opts.fields:

<type 'exceptions.AttributeError'>: 'ProductForm' object has no
attribute 'cleaned_data'
--

which tells me that something isn't validating, but no answers as to
what. productform.errors in the shell returns nothing. The error
occurs even if don't change anything on the form, and still happens if
I include the 'user' field.

In short, is there anything obvious that I'm missing? The site user
has permission to add, change and delete in the model. The database is
Mysql and the database user has select, create, insert, update and
delete privileges.

I have to admit that I'm very frustrated with the Forms documentation
as it seems to assume that everyone wants standard forms with no
custom output, which I would regard to be the exception rather than
the norm. The lack of usable error reporting also seems to be a
fundamental problem in an important part of the framework. Or perhaps
it's something else I'm missing.

TIA
Simon

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to