Re: Model.clean() ValidationError

2011-03-15 Thread Ion Ray Studios

On 15/03/11 14:56, Tom Evans wrote:

On Tue, Mar 15, 2011 at 2:36 PM, Ben Dembroski  wrote:

Hi Tom,

Thanks for your reply. (You're right, my example was lousy).

I did in fact try what you suggested earlier on, and got the the
following error:

AttributeError at /people/add/

'ValidationError' object has no attribute 'message_dict'

Request Method: POST
Request URL:http://localhost:8000/people/add/
Django Version: 1.3 rc 1 SVN-15755
Exception Type: AttributeError
Exception Value:

'ValidationError' object has no attribute 'message_dict'

Exception Location: /home/benjamin/Ion/ellieharrison/trajectories/
views.py in playeradd, line 298
Python Executable:  /usr/bin/python
Python Version: 2.6.6
Python Path:

['/home/benjamin/Ion/ellieharrison',
  '/usr/lib/python2.6',
  '/usr/lib/python2.6/plat-linux2',
  '/usr/lib/python2.6/lib-tk',
  '/usr/lib/python2.6/lib-old',
  '/usr/lib/python2.6/lib-dynload',
  '/usr/lib/python2.6/dist-packages',
  '/usr/lib/python2.6/dist-packages/PIL',
  '/home/benjamin/django-trunk',
  '/usr/lib/python2.6/dist-packages/gst-0.10',
  '/usr/lib/pymodules/python2.6',
  '/usr/lib/python2.6/dist-packages/gtk-2.0',
  '/usr/lib/pymodules/python2.6/gtk-2.0']

Server time:Tue, 15 Mar 2011 14:27:13 +

I have this in the code:
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS

and the code is currently so:
except ValidationError, e:
errormsg = e.message_dict[NON_FIELD_ERRORS]

form = EditPerson()
return render_to_response('personentry.html', { 
'form' : form,
'errormsg': errormsg  },context_instance=RequestContext(request))


Here's the full traceback:

Environment:


Request Method: POST
Request URL: http://localhost:8000/people/add/

Django Version: 1.3 rc 1 SVN-15755
Python Version: 2.6.6
Installed Applications:
['django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.sites',
  'django.contrib.messages',
  'django.contrib.admin',
  'trajectories']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
  'django.contrib.sessions.middleware.SessionMiddleware',
  'django.middleware.csrf.CsrfViewMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/home/benjamin/django-trunk/django/core/handlers/base.py" in
get_response
  111. response = callback(request,
*callback_args, **callback_kwargs)
File "/home/benjamin/django-trunk/django/contrib/auth/decorators.py"
in _wrapped_view
  23. return view_func(request, *args, **kwargs)
File "/home/benjamin/Ion/ellieharrison/trajectories/views.py" in
playeradd
  298.  errormsg = e.message_dict[NON_FIELD_ERRORS]

Exception Type: AttributeError at /people/add/
Exception Value: 'ValidationError' object has no attribute
'message_dict'


Hmm, I think the docs are, if not wrong, misleading. From looking at
the code (1.2/trunk), ValidationError only has an attribute
message_dict if it is constructed with a dictionary (I let out an
audible 'ewww' as I read the code).

If it is constructed with a string or a list of strings, then it
should have an attribute messages, which is a simple list of strings.

There is also a method update_error_dict(). I think using this, you
can write some code which works in all cases:

from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
try:
   ...
except ValidationError, e:
   message_dict = e.update_error_dict({})
   print message_dict[NON_FIELD_ERRORS]

Just while I'm on this class, the update_error_dict() method will
happily take None as an argument if it is constructed with a
dictionary (by design, it checks for it), but blows up if it is
constructed with a list or a string:


e=ValidationError({'foo': 'bar'})
e.update_error_dict(None)

{'foo': 'bar'}

e=ValidationError('foo bar')
e.update_error_dict(None)

Traceback (most recent call last):
   File "", line 1, in
   File "django/core/exceptions.py", line 85, in update_error_dict
 error_dict[NON_FIELD_ERRORS] = self.messages
TypeError: 'NoneType' object does not support item assignment


This class is horrendous - however it is constructed, it should
present a consistent interface. It can't even present a consistent
method.

Dirty, Bad and Wrong.

Cheers

Tom


Thanks Tom.

Did the trick!

--
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.



Re: Model.clean() ValidationError

2011-03-15 Thread Ion Ray Studios

On 15/03/11 14:42, Daniel Roseman wrote:

On Tuesday, March 15, 2011 2:36:45 PM UTC, Ben Dembroski wrote:


Hi Tom,

Thanks for your reply. (You're right, my example was lousy).

I did in fact try what you suggested earlier on, and got the the
following error:

AttributeError at /people/add/

'ValidationError' object has no attribute 'message_dict'


I have this in the code:
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS

and the code is currently so:
except ValidationError, e:
errormsg = e.message_dict[NON_FIELD_ERRORS]

form = EditPerson()
return render_to_response('personentry.html', { 'form' : form,
'errormsg': errormsg  },context_instance=RequestContext(request))


Why are you trying to catch the ValidationError? The whole point is 
that this is caught by the form's `clean()` method and then the 
message appears in the error dictionary.

--
DR.
--
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.

I knew someone was going to ask that :)

Mostly, it's a 'hacky' reason.  In this occasion, the form is collecting 
the date date in 'sections', Year, Month, Day.  Only the Year is 
required.  The month and the day are optional.   If the user leaves the 
month and / or day fields blank, the  view is inserting dummy values 
into the database.  (Don't ask me why, please).


At the moment, all the form is doing is checking to make sure each field 
is an integer, and has the appropriate number of digits in each field.   
However, it's still possible for a user to enter an invalid date -- Like 
1956 - 02 - 31.   The form is valid, but the date is not.  Handling the 
exception being raised by the model validation just seemed easier for 
some reason.


--
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.



Re: cleaned_data not behaving how I expect it to. (newbie confusion)

2011-02-08 Thread Ion Ray Studios

Thanks for the "heads up".

I actually was aware of this. When I'm trying to figure this kind of 
thing out, I start with the simplest problem and then add complications 
once I understand the fundamentals -- even if that means doing it the 
wrong way initially.  Perhaps not the best approach...


Thanks again!

On 08/02/11 09:21, Tom Evans wrote:

On Mon, Feb 7, 2011 at 11:54 PM, Ben Dembroski  wrote:

My apologies to the list.

I just noticed the commas at the end of the lines in the views.py
file.

Once I got rid of those, all was much better.




Glad to hear it. BTW, you are expressly going against how django
suggests you validate two fields that depend upon each other. [1]

Cheers

Tom

[1] 
http://docs.djangoproject.com/en/1.2/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other



--
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.



Re: copying sqlite database file between projects

2011-01-26 Thread Ion Ray Studios
Yup. That did it.  The errors were a result of setttings.py pointing to 
the wrong database.


Too much mucking about with too many things at the same time...


On 25/01/11 16:36, Michael wrote:

I believe table names are all lower-case, so try 'sample_app_person'


--
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.



Re: copying sqlite database file between projects

2011-01-25 Thread Ion Ray Studios
That solved the syncdb error, but I'm still getting the same error 
inside the shell when I am trying get to the data.


Progress!

Thanks again!


On 25/01/11 16:36, Michael wrote:

I believe table names are all lower-case, so try 'sample_app_person'


--
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.