Sunday, January 3, 2016

Re: EmailField: unable to make it mandatory?


On Jan 3, 2016 9:02 PM, "gerard" <gh224391@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.

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUM1KDo0X7VSAuLk2DHYr1rLnMpODYbW_JJ%2BfqnmzgANg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment