ENH: Sex fields as lookups (#55)
This commit is contained in:
parent
4cd133f993
commit
c52d4e736d
9 changed files with 182 additions and 9 deletions
39
ccdb/species/migrations/0004_sex.py
Normal file
39
ccdb/species/migrations/0004_sex.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
from django.db import migrations, models
|
||||
from django.forms import modelform_factory
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
def migrate(apps, schema_editor):
|
||||
Sex = apps.get_model('species', 'Sex')
|
||||
SexForm = modelform_factory(Sex, fields=('name', 'sort_order'))
|
||||
for i, s in enumerate(['male', 'female', 'mixed', 'unknown']):
|
||||
form = SexForm(dict(name=s, sort_order=i))
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
else:
|
||||
print('sex', form.errors.as_data())
|
||||
|
||||
def rollback(apps, schema_editor):
|
||||
Sex = apps.get_model('species', 'Sex')
|
||||
Sex.objects.all().delete()
|
||||
|
||||
dependencies = [
|
||||
('species', '0003_DATA_reset_sequences'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Sex',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True,
|
||||
serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=25, unique=True)),
|
||||
('sort_order', models.IntegerField(blank=True, null=True)),
|
||||
],
|
||||
options={
|
||||
'verbose_name_plural': 'sex',
|
||||
'ordering': ['sort_order'],
|
||||
},
|
||||
),
|
||||
migrations.RunPython(migrate, rollback),
|
||||
]
|
68
ccdb/species/migrations/0005_replace_sex_fields.py
Normal file
68
ccdb/species/migrations/0005_replace_sex_fields.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
def migrate(apps, schema_editor):
|
||||
Sex = apps.get_model('species', 'Sex')
|
||||
CollectionSpecies = apps.get_model('species', 'CollectionSpecies')
|
||||
|
||||
for cs in CollectionSpecies.objects.all():
|
||||
if cs.old_sex:
|
||||
if cs.old_sex == 'both':
|
||||
s = 'mixed'
|
||||
elif cs.old_sex not in ['male', 'female', 'mixed', 'unknown']:
|
||||
s = 'unknown'
|
||||
else:
|
||||
s = cs.old_sex
|
||||
cs.sex = Sex.objects.get(name=s)
|
||||
cs.save()
|
||||
|
||||
def rollback(apps, schema_editor):
|
||||
CollectionSpecies = apps.get_model('species', 'CollectionSpecies')
|
||||
|
||||
for cs in CollectionSpecies.objects.all():
|
||||
if cs.sex:
|
||||
cs.sex = None
|
||||
cs.old_sex = ''
|
||||
cs.save()
|
||||
|
||||
dependencies = [
|
||||
('species', '0004_sex'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='collectionspecies',
|
||||
old_name='sex',
|
||||
new_name='old_sex',
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='trapspecies',
|
||||
old_name='sex',
|
||||
new_name='old_sex',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='collectionspecies',
|
||||
name='sex',
|
||||
field=models.ForeignKey(
|
||||
null=True, on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name='collection_species', to='species.Sex'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='trapspecies',
|
||||
name='sex',
|
||||
field=models.ForeignKey(
|
||||
null=True, on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name='trap_species', to='species.Sex'),
|
||||
),
|
||||
migrations.RunPython(migrate, rollback),
|
||||
migrations.RemoveField(
|
||||
model_name='collectionspecies',
|
||||
name='old_sex',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='trapspecies',
|
||||
name='old_sex',
|
||||
),
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue