django-two-factor-auth icon indicating copy to clipboard operation
django-two-factor-auth copied to clipboard

Problem running migrations with custom User model ('User' object has no attribute 'get_username')

Open AndrejZbin opened this issue 2 years ago • 0 comments

File "/XXX/lib/python3.9/site-packages/two_factor/migrations/0003_auto_20150817_1733.py", line 15, in migrate_phone_numbers
    username = device.user.get_username()
AttributeError: 'User' object has no attribute 'get_username'

Seems like when running the migration, the user instance is of type __fake__.User (historic model) which does not contain method get_username.

Our model:

class User(AbstractBaseUser, PermissionsMixin):
    USERNAME_FIELD = 'email'
    email = models.EmailField(_('email address'), unique=True)
    ...

Didn't try this with default model. But we inherit from AbstractBaseUser which has get_username method and it works outside of migrations.

Expected Behavior

Migration should run without errors (not rely on username field?)

Current Behavior

Migration fails

Possible Solution

Username is only used for logging and is not actually required for migration. Find a different way to access username or change logging to e.g pk instead of USERNAME_FIELD

Context

We are migrating from python2.7 django1.6 to python3.9 django 3.2

Your Environment

  • Python version: 3.9
  • Django version: 3.2
  • django-otp version: 1.1.1
  • django-two-factor-auth version: 1.13.1

AndrejZbin avatar Sep 14 '22 12:09 AndrejZbin