Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If blank=True, it's impossible to save an empty phone number #1

Closed
ludovic-gasc opened this issue Sep 15, 2011 · 9 comments
Closed

Comments

@ludovic-gasc
Copy link

If you define a field like this in your models.py:

    faxnumber = PhoneNumberField(u'Fax',max_length=40,blank=True)

You'll have this backtrace:

Environment:


Request Method: POST
Request URL: http://morphee.hq.eyepea.be:8001/admin/xivo_web/entity/3/

Django Version: 1.3.1
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'xivo_web']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/options.py" in wrapper
  307.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/utils/decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  79.         response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/sites.py" in inner
  197.             return view(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/utils/decorators.py" in _wrapper
  28.             return bound_func(*args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/utils/decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/utils/decorators.py" in bound_func
  24.                 return func(self, *args2, **kwargs2)
File "/usr/lib/python2.5/site-packages/django/db/transaction.py" in inner
  217.                 res = func(*args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/options.py" in change_view
  982.                 self.save_model(request, new_object, form, change=True)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/options.py" in save_model
  665.         obj.save()
File "/usr/lib/python2.5/site-packages/django/db/models/base.py" in save
  460.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/lib/python2.5/site-packages/django/db/models/base.py" in save_base
  526.                         rows = manager.using(using).filter(pk=pk_val)._update(values)
File "/usr/lib/python2.5/site-packages/django/db/models/query.py" in _update
  491.         return query.get_compiler(self.db).execute_sql(None)
File "/usr/lib/python2.5/site-packages/django/db/models/sql/compiler.py" in execute_sql
  869.         cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/usr/lib/python2.5/site-packages/django/db/models/sql/compiler.py" in execute_sql
  735.         cursor.execute(sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/util.py" in execute
  34.             return self.cursor.execute(sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/mysql/base.py" in execute
  86.             return self.cursor.execute(query, args)
File "/var/lib/python-support/python2.5/MySQLdb/cursors.py" in execute
  168.         if not self._defer_warnings: self._warning_check()
File "/var/lib/python-support/python2.5/MySQLdb/cursors.py" in _warning_check
  82.                     warn(w[-1], self.Warning, 3)
File "/usr/lib/python2.5/warnings.py" in warn
  62.                   globals)
File "/usr/lib/python2.5/warnings.py" in warn_explicit
  102.         raise message

Exception Type: Warning at /admin/xivo_web/entity/3/
Exception Value: Column 'faxnumber' cannot be null
@stefanfoulis
Copy link
Owner

empy values are saved as NULL, so you'll need to define the field like this:

faxnumber = PhoneNumberField(u'Fax', null=True, blank=True)

I added an example of an optional field in the README.

@ludovic-gasc
Copy link
Author

Ok, thanks for your comment, but, in the Django documentation, you can find a specific comment about null usage: https://docs.djangoproject.com/en/dev/ref/models/fields/#null
"In most cases, it’s redundant to have two possible values for “no data;” Django convention is to use the empty string, not NULL."
PhoneNumberField is varchar column in the database, this advice should concern PhoneNumberField, isn't it ?
Feel free to reopen or not this issue, it's your project.

Moreover, in your example, you should add this line at the beginning:

   from phonenumber_field.modelfields import PhoneNumberField

@stefanfoulis stefanfoulis reopened this Sep 15, 2011
@stefanfoulis
Copy link
Owner

thanks for the pointer. I added it to the README.

I agree, there is actually no reason anymore to distinguish between None and "".
Originally (I think) I used the two to distinguish between "not set" and "validation failed". But the way the field is structured now, that is no longer needed.

I'll re-open the ticket.

Thanks for reporting :-)

@sethlivingston
Copy link

This confused me, too. Specifying blank=True by itself should make the field optional.

Thank you for developing this field!

@aalexgabi
Copy link

Same problem here. I agree with @sethlivingston.

jjmontalbo added a commit to jjmontalbo/django-phonenumber-field that referenced this issue Mar 21, 2014
@elmit
Copy link

elmit commented Mar 23, 2015

I have another problem with that bug.
I add field to model:

phone = PhoneNumberField(blank=True, unique=True, null=True)

It's generate migration sql (Django 1.7):

ALTER TABLE "account_userprofile" ADD COLUMN "phone" varchar(128) DEFAULT '' NULL UNIQUE;

It's incorrect SQL because when i save new model instance i get an exception (with unique constrain). Correct migration must be:

ALTER TABLE "account_userprofile" ADD COLUMN "phone" varchar(128) NULL UNIQUE;

@jambonrose
Copy link
Contributor

@elmit I believe this is actually a separate bug, having to do with null rather than blank. I've opened issue #102 on the topic.

@amateja
Copy link
Collaborator

amateja commented Feb 29, 2016

It is fixed and tested.

@amateja amateja closed this as completed Feb 29, 2016
amateja pushed a commit that referenced this issue Sep 21, 2016
…patch-1

pt/LC_MESSAGE complete po translaction
amateja pushed a commit that referenced this issue Mar 4, 2017
Support for Brazilian Portuguese
@edevil
Copy link

edevil commented Jan 23, 2018

So I'm using version 2.0.0 of this module and seem to be having the same problem of this issue.

I have a model field defined like:

phone = PhoneNumberField(_('Phone'), blank=True)

However, when I submit a ModelForm without sending a value for "phone", the form is valid but when I try to save the model I get:

django.db.utils.IntegrityError: null value in column "phone" violates not-null constraint

Is this a different problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants