Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
models.fields.Field default value rational
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Rodrigue  
View profile  
 More options Jun 29, 5:12 pm
From: Rodrigue <rodriguealca...@gmail.com>
Date: Mon, 29 Jun 2009 14:12:24 -0700 (PDT)
Local: Mon, Jun 29 2009 5:12 pm
Subject: models.fields.Field default value rational
Hi all,

I came across a behaviour that somewhat surprised me on the model
fields. Namely, if you have a Field with no default value, an empty
value for that field is likely to be turned into the empty string by
Field.get_default.

I came across this behaviour writing unit tests for an applicatioin
I'm working on and where I was expecting my database to complain when
I created and saved an object with no values for one of its
'mandatory' fields. Or at least I expected it to be mandatory.

A quick example:

class DummyModel(models.Model):
    mandatory_field = models.CharField(max_length=50)
    optional_field = models.CharField(max_length=50, blank=True,
null=True)

empty_object = DummyModel()
empty_object.save()

I would expect this code to raise an IntegrityException -- and I'm
pretty sure it used to be the observed behaviour in older versions.
Instead, I find my database having a DummyModel entry with the empty
string value for mandatory_field and null for optional_field.

Is that not going against the 'madatory-ness' of the field
declaration? I have tried to tell: don't allow empty values for this
field and, obviously, it is not really enforced...

Am I expecting something that is not supposed to be provided by the
Field class but by the form class instead? At any rate, I don't see
why my None should be interpreted as the empty string. So here is my
question: what is the rational behind this behaviour?

cheers,
Rodrigue

Now, the


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Evans  
View profile  
 More options Jun 30, 4:34 am
From: Tom Evans <tevans...@googlemail.com>
Date: Tue, 30 Jun 2009 09:34:30 +0100
Local: Tues, Jun 30 2009 4:34 am
Subject: Re: models.fields.Field default value rational

max_length=50 and blank=True are validations that are checked by the
form API/the admin interface. They aren't used to validate at the DB
layer (nothing is).

Re-read http://docs.djangoproject.com/en/dev/ref/models/fields/ , which
also explains the rational to why passing mandatory_field=None to the
constructor results in an empty string being stored for mandatory_field.

Cheers

Tom


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google