I am adding the following attribute type choice
to my model
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,)
When I execute the migration, it tells me the following:
(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) ✗
It tells me that the attribute or column gender
that I am adding already contains a null value, but I do not understand if a choice type field from a tuple should be specified more when defining it.
I'm following the example as Django recommends but I do not know what my error.
The code of the migration that you are trying to apply is this:
# -*- 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),
),
]
On the other hand, which is more recommendable, that in a form the user chooses the gender (male and female) using a select box or is it better an item type radio button?