rename unique field in django model and migrate










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?










share|improve this question






















  • 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















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?










share|improve this question






















  • 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













0












0








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?










share|improve this question














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?







python django django-models django-1.11






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 10:56









cardamomcardamom

1,95511342




1,95511342












  • 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

















  • 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
















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





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 Answer
1






active

oldest

votes


















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.






share|improve this answer

























  • 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 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.

    – 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. docs.djangoproject.com/en/2.1/topics/auth/customizing/…

    – 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298570%2frename-unique-field-in-django-model-and-migrate%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer

























  • 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 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.

    – 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. docs.djangoproject.com/en/2.1/topics/auth/customizing/…

    – 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















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.






share|improve this answer

























  • 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 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.

    – 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. docs.djangoproject.com/en/2.1/topics/auth/customizing/…

    – 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













1












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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 16:03









cardamom

1,95511342




1,95511342










answered Nov 14 '18 at 11:37









MaxMax

82531022




82531022












  • 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 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.

    – 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. docs.djangoproject.com/en/2.1/topics/auth/customizing/…

    – 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, 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 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.

    – 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. docs.djangoproject.com/en/2.1/topics/auth/customizing/…

    – 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, 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





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 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.

– cardamom
Nov 14 '18 at 13:25





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.

– 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. docs.djangoproject.com/en/2.1/topics/auth/customizing/…

– Max
Nov 14 '18 at 14:19






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/…

– 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





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, 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



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298570%2frename-unique-field-in-django-model-and-migrate%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo