Sunday, September 30, 2012

Re: model field not null

null=True is the opposite of what you are asking for anyway. You can
use null=False, and the column will be created with the database's
version of:

NOT NULL

Which is the postgres spelling.

This will prevent the database from allowing a database NULL from
being stored there.

But it will NOT prevent storing an empty string.

Assuming that your database supports it, you could write a "trigger"
or "constraint" that checks for non blank and fails the transaction
otherwise. But, in so far as I know, Django provides no help in
creating this constraint.

Bill

On Wed, Sep 26, 2012 at 4:47 PM, Jonas Geiregat <jonas@geiregat.org> wrote:
> Hello,
>
> While setting up a new model I'm trying to have a strong data model.
>
> One of the requirements is that a certain field cannot be null.
>
> lastname = models.CharField(
> blank=False,
> default=None,
> ..)
>
> I'm accomplishing this by the above code. This ensure that even on a database level lastname will not be NULL, by setting default to None.
>
> I'm aware that I can just use blank=False to ensure that on a form level it will never be allowed to be empty. But I want a strong, fully tested, business model from the beginning.
>
> As this is a CharField, according to the docs, I cannot use null=True since empty string will get stored as empty strings and not as NULL.
>
> Is this an appropriate way of modeling my models, or are there other solutions ?
>
> Regards,
>
> Jonas.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment