Estoy adicionando el siguiente atributo tipo choice
a mi modelo
class User(AbstractUser):
GENDER_MALE = 'M'
GENDER_FEMALE = 'F'
GENDER_OTHER = 'O'
GENDER_CHOICES = (
(GENDER_MALE, u'male'),
(GENDER_FEMALE, u'female'),
(GENDER_OTHER, u'other'),
)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default=GENDER_OTHER,)
Cuando yo ejecuto la migración, me dice lo siguiente:
(nrb_dev)➜ project git:(master) ✗ python manage.py makemigrations
Migrations for 'userprofile':
0010_auto_20160116_2219.py:
- Alter field gender on user
(nrb_dev)➜ project git:(master) ✗ python manage.py migrate
Operations to perform:
Apply all migrations: sessions, userprofile, auth, admin, contenttypes
Running migrations:
Applying userprofile.0006_auto_20160116_2206...Traceback (most recent call last):
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.IntegrityError: column "gender" contains null values
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 396, in add_field
self.execute(sql, params)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: column "gender" contains null values
(nrb_dev)➜ project git:(master) ✗
Me dice que el atributo o columna gender
que estoy adicionando ya contiene un valor nulo, pero no entiendo si a un campo tipo choice a partir de una tupla se le deba especificar algo más al definirlo.
Estoy siguiendo el ejemplo como Django lo recomienda pero no se cual es mi error.
El código de la migración que se intenta aplicar es este:
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2016-01-16 22:19
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('userprofile', '0009_auto_20160116_2214'),
]
operations = [
migrations.AlterField(
model_name='user',
name='gender',
field=models.CharField(choices=[('M', 'male'), ('F', 'female'), ('O', 'other')], default='O', max_length=1),
),
]
Por otro lado, que es mas recomendable, ¿que en un formulario el usuario escoja el género (masculino y femenino) utilizando un select box o es mejor un item tipo radio button?
0010_auto_20160116_2219.py
. Me gustaría verlo antes de soltar algunas ideasnull=True
?django_content_type
que corresponda alapp_label
ymodel
de tu tabla, con ese ID borra los registros enauth_permission
correspondientes acontent_type_id
, luego borras el registro de la tabladjango_content_type
. Imagino que debería funcionar.python manage.py migrate
y me funcionó, pero me queda de lección aplicar y desaplicar las migraciones, es lo correcto. Podŕia haber podido cuadrar el error manualmente, pero creo que algo hice malo en borrar algunos registros. GRacias :D