67👍
Here is an example doing everything in one single migration thanks to a RunPython call.
# -*- coding: utf-8 -*
from __future__ import unicode_literals
from django.db import migrations, models
import uuid
def create_uuid(apps, schema_editor):
Device = apps.get_model('device_app', 'Device')
for device in Device.objects.all():
device.uuid = uuid.uuid4()
device.save()
class Migration(migrations.Migration):
dependencies = [
('device_app', 'XXXX'),
]
operations = [
migrations.AddField(
model_name='device',
name='uuid',
field=models.UUIDField(blank=True, null=True),
),
migrations.RunPython(create_uuid),
migrations.AlterField(
model_name='device',
name='uuid',
field=models.UUIDField(unique=True)
)
]
36👍
(Answer taken from the first comment)
See the django docs – Migrations that add unique fields
They recommend changing your single migration into three separate migrations:
- Create field, set to null but not unique
- Generate unique UUIDs
- Alter the field to be unique
- [Django]-The QuerySet value for an exact lookup must be limited to one result using slicing. Filter error
- [Django]-How to add custom field in ModelSerializer?
- [Django]-Django admin TabularInline – is there a good way of adding a custom html column?
4👍
In the mode, you have configured, that you want unique values for the uuid fields, but with default values(the same for all). So if you have two ‘device’ objects in the database, the migrations add ‘uuid’ field to them with the default ‘uuid.uuid4’ value and when it tries to set it to the second one, it crashes because of the unique constrains.
If you drop your db and create new objects probably there will be not problems but thats not a solution for production db obviously :D.
A better solution is to create a data migration which sets different uuid value (generated by the default ‘uuid’ library) to every existing object in the database. You can read more about data migrations here:
https://docs.djangoproject.com/en/1.10/topics/migrations/#data-migrations
Then, when you create new objects, django will generate different uuid automatically. 😉
For the primary keys: Django adds it to the model by default.
- [Django]-Sending an SMS to a Cellphone using Django
- [Django]-Django in / not in query
- [Django]-Manually logging in a user without password
0👍
You can provide a management command to populate the uuid field after uu_id column is created in the model but this has to be done after migrating the model and setting the field default as None:
from django.core.management.base import BaseCommand
from django.apps import apps
import uuid
class Command(BaseCommand):
def handle(self, *args, **options):
classes()
def classes():
app_models = apps.get_app_config('appname').get_models()
for model in app_models:
field = None
try:
field = model._meta.get_field('uu_id')
except:
pass
if field:
uu_id_list = list(model.objects.all().values_list('uu_id',flat=True))
if None in uu_id_list:
for row in model.objects.all():
row.uu_id = uuid.uuid4()
row.save()
- [Django]-UUID as default value in Django model
- [Django]-Is it secure to store passwords as environment variables (rather than as plain text) in config files?
- [Django]-What is the benefit of using a HyperlinkedModelSerializer in DRF?