26 lines
755 B
Python
26 lines
755 B
Python
from django.db import migrations, models
|
|
|
|
|
|
def populate_code(apps, schema_editor):
|
|
"""为已有机构填充 code = 'INST-{pk}'。"""
|
|
Institution = apps.get_model('user', 'Institution')
|
|
for inst in Institution.objects.filter(code__isnull=True):
|
|
inst.code = f'INST-{inst.pk}'
|
|
inst.save(update_fields=['code'])
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('user', '0003_institution_add_code_field'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(populate_code, migrations.RunPython.noop),
|
|
migrations.AlterField(
|
|
model_name='institution',
|
|
name='code',
|
|
field=models.CharField(max_length=100, unique=True, verbose_name='机构编码'),
|
|
),
|
|
]
|