conditional model validation

35 views
Skip to first unread message

Lachlan Musicman

unread,
Sep 27, 2012, 7:26:01 PM9/27/12
to django...@googlegroups.com
Hola,

I'm searching without much luck and can't see the answer in the docs.

Am wanting to override the is_valid() method on a model (I think
that's what I want).

Basically, I have a choices field, and if one of those choices is
selected, then I want a description field to be not blank/not the
empty string. If it's the other two choices, I want the description
field to be blank/empty string.

First, I override is_valid() as a model method?

Second, how do I return a false from is_valid()?

ie, this is a rough up of what I'm looking for - what goes in place of
return FALSE/TRUE?

def is_valid(self):

if enrolment.mark == 'W': #student has withdrawn from course
-if enrolment.withdrawal_reason == '': #must have reason
--return FALSE
elif enrolment.withdrawal.reason != "": #ie, since enrolment.mark
isn't 'W', confirm that the enrolment.reason is ""
-return FALSE
else:
-return TRUE

Cheers
L.
--
...we look at the present day through a rear-view mirror. This is
something Marshall McLuhan said back in the Sixties, when the world
was in the grip of authentic-seeming future narratives. He said, “We
look at the present through a rear-view mirror. We march backwards
into the future.”

http://www.warrenellis.com/?p=14314

Babatunde Akinyanmi

unread,
Sep 28, 2012, 2:36:18 AM9/28/12
to django...@googlegroups.com
Let me try to assist. A forms is_valid method causes 3 types of
cleaning methods to be run. Based on that, I'm sure you really don't
need to override it.

I really don't understand what you mean by "I want the description
field to be not blank/not the empty string" so maybe I could have been
of more help but I'm sure a solution to your problem would be to
override either the form's clean_fieldname or clean methods.

Check: https://docs.djangoproject.com/en/dev/ref/forms/validation/
which is a link to the documentation on form validation
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

Lachlan Musicman

unread,
Sep 28, 2012, 8:13:55 PM9/28/12
to django...@googlegroups.com
On Friday, September 28, 2012, Babatunde Akinyanmi wrote:
Let me try to assist. A forms is_valid method causes 3 types of
cleaning methods to be run. Based on that, I'm sure you really don't
need to override it.


Yes, I've just discoverde the ValidationError e part of the docs :|

 
I really don't understand what you mean by "I want the description
field to be not blank/not the empty string" so maybe I could have been
of more help but I'm sure a solution to your problem would be to
override either the form's clean_fieldname or clean methods.

ie, 
if e.mark = W, e.descr must be >"" # it can be anything from "a" to "pregnant" to "killed" to "this is not an empty string" or " this is not a blank string"
else e.descr == "" #this is a blank or empty string



 
Check: https://docs.djangoproject.com/en/dev/ref/forms/validation/
which is a link to the documentation on form validation


cheers - since I am using the admin forms, I was hoping I could add it to the model, but is ok if I need to rewrite the forms

cheers
L.
Reply all
Reply to author
Forward
0 new messages