

The "default" option Django 1.8 only executes the external function on...
source link: https://www.codesd.com/item/the-default-option-django-1-8-only-executes-the-external-function-once.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The "default" option Django 1.8 only executes the external function once
I am trying to add a UUID
field to an existing table. I specified that default = uuid.uuid4
however, it Django doesn't seem to call uuid.uuid4
function for every row. So when I migrate I keep getting duplicated uuid error.
My Django
version is 1.8.2
.
from django.db import models, migrations
import uuid
class Migration(migrations.Migration):
dependencies = [
('conv', '0008_video_video_uri'),
]
operations = [
migrations.AddField(
model_name='conversation',
name='channel_id',
field=models.UUIDField(unique=True, default=uuid.uuid4, editable=False),
),
]
Below the error:
> > File "/home/yonk/projects/trailerapp/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py",
> line 64, in execute
> return self.cursor.execute(sql, params) django.db.utils.IntegrityError: could not create unique index
> "conv_conversation_channel_id_68f7d58df7c78d61_uniq" DETAIL: Key
> (channel_id)=(5f512cbe-e514-4bf5-bf5a-3efd1a94e401) is duplicated.
Here you have django docs describing exactly what you want: https://docs.djangoproject.com/en/1.8/howto/writing-migrations/#migrations-that-add-unique-fields
You will need two migration files.
- First one Adds fields, also change unique=True to null=True so django won't try to use default value...
- Second migration populates the field.
So second migration should look like this:
def gen_uuid(apps, schema_editor):
MyModel = apps.get_model('myapp', 'MyModel')
for row in MyModel.objects.all():
row.uuid = uuid.uuid4()
row.save()
class Migration(migrations.Migration):
dependencies = [
('myapp', '0004_add_uuid_field'),
]
operations = [
# omit reverse_code=... if you don't want the migration to be reversible.
migrations.RunPython(gen_uuid, reverse_code=migrations.RunPython.noop),
]
Recommend
-
71
README.md DLR DLR is a stand-alone, light-weight and portable runtime for CNN and decicion-tree models. Built on top of TVM
-
42
See how your code actually executes with Stackdriver Profiler, now GA...
-
27
Privacy-oriented cryptocurrency Grin has just executed its first backward-incompatible upgrade, also called a hard fork. Today’splanned upgrade introduces key changes to the nearly $60-million network that will op...
-
7
Apple executes New Year's Eve apps purge in ChinaBy Leo KelionTechnology desk editorPublishedduration5 hours agoimage copyrightGetty ImagesApple k...
-
3
Wasm3 on Twitter: "Ok, so here #Wasm3 compiles itself (using #Clang compiled to #WebAssembly), and executes another #WASI app in self-hosting mode. 😁 Execution time includes the build time, obviously! https://t.co/NttJ9l7KXh"Don’t miss what...
-
9
How Spark Internally Executes A Program Reading Time: 3 minutesHello everyone! In my previous blog, I explained the difference between RDD, DF, and DS you can f...
-
12
Ant executes error when copying advertisements I need to concatenate two files. I use Ant's exec for this purpose, but I get the following err...
-
6
Have Vagrant + Chef executes the referral if the template files have been checked with the CRLF end of line advertisements I keep getting bit...
-
8
Tennis champ executes the FIFA Dead Fish celebration after winning the US Open 'Only legends will understand what I did after the match is L2 plus left' By...
-
7
Interview with Misko Hevery, Chief Technology Officer at Builder.io "Qwik's magic is not in how fast it executes, but how good it is in avoiding doing any work."
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK