Django fixtures save with default value
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping
--
Chapters
00:00 Django Fixtures Save With Default Value
02:29 Accepted Answer Score 7
03:03 Answer 2 Score 2
03:29 Answer 3 Score 1
03:51 Thank you
--
Full question
https://stackoverflow.com/questions/2572...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #fixtures #python34 #django17
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping
--
Chapters
00:00 Django Fixtures Save With Default Value
02:29 Accepted Answer Score 7
03:03 Answer 2 Score 2
03:29 Answer 3 Score 1
03:51 Thank you
--
Full question
https://stackoverflow.com/questions/2572...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #fixtures #python34 #django17
#avk47
ACCEPTED ANSWER
Score 7
The solution was to use django signals:
import uuid
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.db.models.signals import pre_save
from django.dispatch import receiver
class Uuidable(models.Model):
uuid = models.CharField(_('uuid'), blank=True,
null=False, unique=True,
max_length=64, default=uuid.uuid4())
class Meta:
abstract = True
@receiver(pre_save)
def set_uuid_on_save(sender, instance, *args, **kwargs):
if instance.pk is None:
instance.uuid = uuid.uuid4()
That way, the model/data is populated whatever way you create the model (via shell, fixtures, whatever).
ANSWER 2
Score 2
Automatically loading initial data fixtures is deprecated in Django 1.7. One solution is via signals as you mentioned. Another one that I prefer is to create a python script where you create all the needed data, and execute it in the shell:
python manage.py shell < create_initial_data.py
ANSWER 3
Score 1
I think that the problem is when you put default=uuid.uuid4(). The parenthesis are too much, because they imply that you pass the result of uuid.uuid4() to default argument and not the function itself, so you should put default=uuid.uuid4.