Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 June 22, 2010 21:11:10

Jonathan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Fields validating as optional


What is the preferred way to make e.g. a TextField that will pass validation
if it is left empty? I've seen two approaches apparently referenced in the
documentation:

additional_comments = models.TextField(required = False)

additional_comments = models.TextField(blank = True)

and run into errors with the first. Does this mean that I should go with the
second, or is there another way that is preferred?

I'm using 1.2.

--
→ Jonathan Hayward, christos.jonathan.hayw...@gmail.com
→ An Orthodox Christian author: theology, literature, et cetera.
→ My award-winning collection is available for free reading online:
☩ I invite you to visit my main site athttp://JonathansCorner.com/--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 22, 2010 21:37:03

Dan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Fields validating as optional


Jonathan,

I think what you are running into is the differences between how
models are declared vs how forms are declared. In models you declare
the fields as blank=True and/or null=True to specify that the data
stored in the database can be left blank. Something like this:

class MyModel(models.Model):
additional_comments = models.TextField(blank=True)

When declaring a form, nothing is directly stored in the database so
the keyword 'required' is used. For example:

class MyForm(forms.Form):
additional_comments = forms.TextField(required=False)

If you are creating a form from a model, blank=True inside the models
is transformed into required=False for the form.

class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel

Hope this helps!

Dan Harris
dih0...@gmail.com


On Jun 22, 4:11 pm, Jonathan Hayward
<christos.jonathan.hayw...@gmail.com> wrote:
> What is the preferred way to make e.g. a TextField that will pass validation
> if it is left empty? I've seen two approaches apparently referenced in the
> documentation:
>
>     additional_comments = models.TextField(required = False)
>
>     additional_comments = models.TextField(blank = True)
>
> and run into errors with the first. Does this mean that I should go with the
> second, or is there another way that is preferred?
>
> I'm using 1.2.
>
> --
> → Jonathan Hayward, christos.jonathan.hayw...@gmail.com
> → An Orthodox Christian author: theology, literature, et cetera.
> → My award-winning collection is available for free reading online:
> ☩ I invite you to visit my main site athttp://JonathansCorner.com/--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 22, 2010 21:45:23

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Fields validating as optional


On Jun 22, 9:11 pm, Jonathan Hayward
<christos.jonathan.hayw...@gmail.com> wrote:
> What is the preferred way to make e.g. a TextField that will pass validation
> if it is left empty? I've seen two approaches apparently referenced in the
> documentation:
>
>     additional_comments = models.TextField(required = False)
>
>     additional_comments = models.TextField(blank = True)
>
> and run into errors with the first. Does this mean that I should go with the
> second, or is there another way that is preferred?
>
> I'm using 1.2.

The first is just wrong, and certainly isn't documented in that form.
You seem to have wrongly conflated some of the syntax from the forms
documentation with the models docs.

Model fields take blank=True/False. Form fields take
'required=False' (or True) as a parameter. However, the field in that
case would be forms.CharField, not models.TextField.
--
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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 23, 2010 03:10:12

Jonathan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Fields validating as optional


Thanks, Dan and Daniel.

On Tue, Jun 22, 2010 at 3:45 PM, Daniel Roseman <dan...@roseman.org.uk>wrote:

> On Jun 22, 9:11 pm, Jonathan Hayward
> <christos.jonathan.hayw...@gmail.com> wrote:
> > What is the preferred way to make e.g. a TextField that will pass
> validation
> > if it is left empty? I've seen two approaches apparently referenced in
> the
> > documentation:
> >
> > additional_comments = models.TextField(required = False)
> >
> > additional_comments = models.TextField(blank = True)
> >
> > and run into errors with the first. Does this mean that I should go with
> the
> > second, or is there another way that is preferred?
> >
> > I'm using 1.2.
>
> The first is just wrong, and certainly isn't documented in that form.
> You seem to have wrongly conflated some of the syntax from the forms
> documentation with the models docs.
>
> Model fields take blank=True/False. Form fields take
> 'required=False' (or True) as a parameter. However, the field in that
> case would be forms.CharField, not models.TextField.
> --
> 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-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>


--
→ Jonathan Hayward, christos.jonathan.hayw...@gmail.com
→ An Orthodox Christian author: theology, literature, et cetera.
→ My award-winning collection is available for free reading online:
☩ I invite you to visit my main site athttp://JonathansCorner.com/--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 20th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support