0

I had a model which looks like this:

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    username = models.TextField(max_length=100, unique=True)
    # other fields

Then I realised after working further on what I am building, that it is quite important that the username field is slightly differently named, so made an alteration in that line:

    username_internal = models.TextField(max_length=100, unique=True)

and ran python manage.py makemigrations myapp.

It asked me for a default value, but when I look at the .py migration it created, don't like what it has done:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2018-11-14 10:49
from __future__ import unicode_literals

import django.contrib.auth.validators
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('myapp', '0002_user_internalid'),
    ]

    operations = [
        migrations.AddField(
            model_name='user',
            name='username_internal',
            field=models.TextField(default=5, max_length=100, unique=True),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='user',
            name='username',
            field=models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username'),
        ),
    ]

It is trying to create a new field, I just want it to rename the existing field.

Am new to Django (using 1.11.4) Does anyone know how to fix this?

cardamom
  • 6,873
  • 11
  • 48
  • 102
  • I think @Max 's solution would have worked if the field in the original model had been called anything but 'username'. Am new to Django, but I think this has overwritten instead of just adding to Django's standard user model and let to some problems with renaming. – cardamom Nov 14 '18 at 13:34

1 Answers1

1

Due to you subclassing AbstractUser it is always including the username field so isnt picking up the rename for the migrations. You will need to change your user model class to look something like this:

from django.contrib.auth.models import AbstractBaseUser

class User(AbstractBaseUser):

    username_internal = models.TextField(max_length=100, unique=True)
    ...

    USERNAME_FIELD = 'username_internal'

    ...

    # you will also need to the user manager `objects = UserManager()`
    # you may be able to import and use the existing user manager from `django.contrib.auth.models import UserManager` depending on your other fields.

A full example is available on https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#a-full-example.

You may need to re-implement permissions methods depending on your use case. See PermissionsMixin.

cardamom
  • 6,873
  • 11
  • 48
  • 102
Max
  • 1,175
  • 3
  • 12
  • 22
  • Thanks, I modified the migration and ran `python manage.py migrate` it looked like it ran okay, but seems to have broken something, am trying to work out what.. – cardamom Nov 14 '18 at 11:59
  • I think you must be right given [this](https://gist.github.com/dhbradshaw/e2bdeb502b0d0d2acced) although maybe it should be "alter" as described [here](https://stackoverflow.com/a/37942618/4288043). Maybe it's a peculiarity of using Django 1.11 (not my choice). Symptom is that new column 'username_internal' is created instead of a renaming! I think the problem is more likely that username 'TextField' had overridden Django's standard user 'Charfield'. I give up on renaming for now if it's going to be this difficult, rolled back to previous migration. – cardamom Nov 14 '18 at 13:25
  • Oh yeah. I see why that would happen. Its because you are subclassing `AbstractUser` which always includes the `username` field. To change the name of the username field you have to create your user class slightly differently. They have an example in the Django docs. https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#a-full-example – Max Nov 14 '18 at 14:19
  • I've updated my answer. Hopefully that will point you in the correct direction this time ;P – Max Nov 14 '18 at 14:31
  • Thanks, will test/adapt it once I finish the current thought and will hopefully be the end of it. – cardamom Nov 14 '18 at 14:33
  • Thanks, it worked. It is a slimmer user, 9 fields were removed in the migration, date_joined, first_name, groups, is_active, is_staff, is_superuser, last_name and user_permissions, but have learned something about it, if I want any of those fields back one day will put them back in manually. [Two user classes explained here](https://stackoverflow.com/questions/21514354/difference-between-abstractuser-and-abstractbaseuser-in-django) – cardamom Nov 14 '18 at 14:57