After changing User.username to PhoneNumberField I can't migrate

51 views
Skip to first unread message

Ahmed Shahwan

unread,
Nov 6, 2019, 7:38:17 AM11/6/19
to Django users
Hello everybody,

I work on a project started from the cookiecutter-django, and I altered the username of the User model to use `PhoneNumberField` from django-phonenumber-field package instead of the ordinary `models.CharField` and made the necessary changes, then I issued `manage.py makemigrations` which worked fine.

But when I run `manage.py migrate` I get this error:

Traceback (most recent call last):
  File "./manage.py", line 30, in <module>
    execute_from_command_line(sys.argv)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 257, in handle
    self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps, plan=plan,
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/core/management/sql.py", line 51, in emit_post_migrate_signal
    **kwargs
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in send
    for receiver in self._live_receivers(sender)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/guardian/management/__init__.py", line 33, in create_anonymous_user
    User.objects.get(**lookup)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/db/models/query.py", line 399, in get
    clone = self.filter(*args, **kwargs)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/db/models/query.py", line 892, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1290, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1318, in _add_q
    split_subq=split_subq, simple_col=simple_col,
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1251, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1116, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/db/models/lookups.py", line 20, in __init__
    self.rhs = self.get_prep_lookup()
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/django/db/models/lookups.py", line 70, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
  File "/home/ahsh/.local/share/virtualenvs/communities_dj_backend/lib/python3.7/site-packages/phonenumber_field/modelfields.py", line 83, in get_prep_value
    raise ValueError("“%s” is not a valid phone number." % value.raw_input)
ValueError: “AnonymousUser” is not a valid phone number.


I googled for a while and don't get what's going on. Any ideas?

Thanks.

Laura Pérez

unread,
Nov 6, 2019, 7:46:04 AM11/6/19
to django...@googlegroups.com

I was having a similar error yesterday, after adding another field to my model I ran the migrations and was giving me an error as if I was overwriting another field. 
After a time searching for an answer for me ,the solution was (and I know this is not a good practice) delete all the migration files and run the migrations again. 

Then again, If someone knows a better solution... 

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1fb0dd24-9d04-4432-ae7b-64b11f160855%40googlegroups.com.
--
--
Laura Pérez Vera
(+53) 53440970 - lpve...@gmail.com

Ahmed Shahwan

unread,
Nov 6, 2019, 8:44:24 AM11/6/19
to Django users
Thank you, Laura, for your input.
I found the problem related to the `django-guardian` pacakge which seems to create an anonymous user when I issue `manage.py migrate` which has a username of "AnonymousUser" and that, of course, doesn't pass the validation error of `PhoneNumberField` which has to be a valid phone number.

the full problem is described here if you're curious to take a look:

Thanks again.


On Wednesday, 6 November 2019 14:46:04 UTC+2, Laura Pérez wrote:
I was having a similar error yesterday, after adding another field to my model I ran the migrations and was giving me an error as if I was overwriting another field. 
After a time searching for an answer for me ,the solution was (and I know this is not a good practice) delete all the migration files and run the migrations again. 

Then again, If someone knows a better solution... 

To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Laura Pérez

unread,
Nov 6, 2019, 9:26:56 AM11/6/19
to django...@googlegroups.com
Great! thanks for sharing , I'm going to see the link.


To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/18d8dbd8-3a6b-4a42-a4ee-8713a6b2eb69%40googlegroups.com.


--
----
Laura Pérez Vera
Mestrado em Ciença da Computação
Universidade Federal de Viçosa, Brazil
Reply all
Reply to author
Forward
0 new messages