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?

1
  • 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. Commented Nov 14, 2018 at 13:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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..
I think you must be right given this although maybe it should be "alter" as described here. 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.
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. docs.djangoproject.com/en/2.1/topics/auth/customizing/…
I've updated my answer. Hopefully that will point you in the correct direction this time ;P
Thanks, will test/adapt it once I finish the current thought and will hopefully be the end of it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.