EmailField: unable to make it mandatory?

22 views
Skip to first unread message

gerard

unread,
Jan 4, 2016, 12:02:56 AM1/4/16
to django...@googlegroups.com
Hello all and happy new year,

with Django 1.7, i have this model:
class Subscription(models.Model):
email = models.EmailField(max_length=75)
created_by = models.ForeignKey(User)

sub = Subscription.objects.create(created_by=self.user)
this does not product an error due to email missing.

I also tried:
email = models.EmailField(max_length=75, null=False, blank=False)
without success

The database is postgresql, the command "manage.py sql" shows:
CREATE TABLE "eventsmgmt_subscription" (
"email" varchar(75) NOT NULL,

in fact, the email is filled with a null string ''.

How can i deal with this?


Thanks in advance for help,

--
Gérard Henry

James Schneider

unread,
Jan 4, 2016, 2:56:37 AM1/4/16
to django...@googlegroups.com

Did you run 'makemigrations' and 'migrate' after making those changes to your model? I suspect you didn't.

The output of the 'sql' command only shows what would be run if you ran a 'migrate' command, and does not reflect the current state of the database.

The reason the .create() command is succeeding is because the database is allowing the record to be saved. The restrictions you placed in the models.py file only pertain to the DB itself (and forms), and are not checked before a direct save attempt is made. That only happens if validators are specified at the model level.

Try creating a new migration and applying it.

Run  'migrate' anyway even if 'makemigrations' doesn't find any changes. If that's the case, that means you made a new migration but didn't apply it to the database, likely explaining your behavior.

-James

gerard

unread,
Jan 4, 2016, 3:06:39 AM1/4/16
to django...@googlegroups.com
Le 04/01/2016 08:55, James Schneider a écrit :
>
> On Jan 3, 2016 9:02 PM, "gerard" <gh22...@gmail.com
> <mailto:gh22...@gmail.com>> wrote:
> >
> > Hello all and happy new year,
> >
> > with Django 1.7, i have this model:
> > class Subscription(models.Model):
> > email = models.EmailField(max_length=75)
> > created_by = models.ForeignKey(User)
> >
> > sub = Subscription.objects.create(created_by=self.user)
> > this does not product an error due to email missing.
> >
> > I also tried:
> > email = models.EmailField(max_length=75, null=False, blank=False)
> > without success
> >
> > The database is postgresql, the command "manage.py sql" shows:
> > CREATE TABLE "eventsmgmt_subscription" (
> > "email" varchar(75) NOT NULL,
> >
> > in fact, the email is filled with a null string ''.
> >
> > How can i deal with this?
> >
> >
> > Thanks in advance for help,
> >
> > --
> > Gérard Henry
>
> Did you run 'makemigrations' and 'migrate' after making those changes to
> your model? I suspect you didn't.
>

yes, i did

> The output of the 'sql' command only shows what would be run if you ran
> a 'migrate' command, and does not reflect the current state of the database.
>

i looked into database directly:
foo=> \d eventsmgmt_subscription
Table «
public.eventsmgmt_subscription »
Colonne | Type |
Modificateurs
---------------------+--------------------------+--------------------------------------------------------------------------
id | integer | non NULL Par défaut,
nextval('eventsmgmt_subscription_id_seq'::regclass)
created_by_id | integer | non NULL
email | character varying(75) | non NULL

as you can see, "not NULL" is in the table


thanks for reply,

--
Gérard Henry

James Schneider

unread,
Jan 4, 2016, 3:25:49 AM1/4/16
to django...@googlegroups.com
i looked into database directly:
foo=> \d eventsmgmt_subscription
                                         Table « public.eventsmgmt_subscription »
       Colonne       |           Type           |         Modificateurs
---------------------+--------------------------+--------------------------------------------------------------------------
 id                  | integer                  | non NULL Par défaut, nextval('eventsmgmt_subscription_id_seq'::regclass)
 created_by_id       | integer                  | non NULL
 email               | character varying(75)    | non NULL

as you can see, "not NULL" is in the table


thanks for reply,

--
Gérard Henry

*smacks forehead* Yes, you're right. I apologize for the red herring. I forgot that we're dealing with a CharField.

It dawned on me as soon as I read this. TheCharField (and others) will use an empty string ("") as their 'empty' value, as opposed to None/null. This is a design decision of Django: 


That means if you want to enforce having any CharField to be required at the model level, you'll need to define a custom validation function to do so.


In general, though, this type of validation is usually done at the form and sometimes view level where end-user input is filtered before being sent to the model code, since the models can sometimes be populated by other sources such as a CSV import that may have different requirements than a standard end-user. But that may not be applicable in your case. 

-James
 
Reply all
Reply to author
Forward
0 new messages