Django Records Saved with Default Value

I am using Django 1.7 and I have a problem with my lights.

I would like Django to use the default value or use the save() method to create undefined values.

Here are my current objects:

 # File: uuidable.py import uuid from django.db import models from django.utils.translation import ugettext_lazy as _ class Uuidable(models.Model): uuid = models.CharField(_('uuid'), blank=True, null=False, unique=True, max_length=64, default=uuid.uuid4()) # Tried here class Meta: abstract = True def save(self, *args, **kwargs): if self.pk is None: self.uuid = uuid.uuid4() # Tried here also super().save(*args, **kwargs) # File: timestampable.py from django.db import models from django.utils.translation import ugettext_lazy as _ class Timestampable(models.Model): created_at = models.DateTimeField(_('created at'), auto_now_add=True) updated_at = models.DateTimeField(_('updated at'), auto_now=True) class Meta: abstract = True # File: post.py from project.lib.models.timestampable import Timestampable from project.lib.models.uuidable import Uuidable class Post(Timestampable, Uuidable): title = models.CharField(_('title'), max_length=250, blank=False) content = models.TextField(_('content')) def __str__(self): return self.title 

As you can see, when generating a new Post() , the created_at , updated_at and uuid values โ€‹โ€‹are automatically created on save() . But when I use appliances, I get the following error:

 [...]initial_data.yaml': Could not load post.Post(pk=None): UNIQUE constraint failed: post_post.uuid 

If I specify uuid in my fixture file, I get a created_at error message and then updated_at . Therefore, I must indicate the contents of each field, even if I want it to be โ€œautomaticโ€.

From the documentation (why is it in django admin docs ?!), I know that the save() method is not called so therefore everything that I entered in the save() method does not work. But should you not enable / use the default or auto_now* functions?

When the archive files are processed, the data is saved in the database as is. The simulated save () methods are not called, and any pre_save or post_save signals will be called with raw = True, since the instance contains only attributes that are local to the model. For example, you can disable handlers that access the associated fields that are present at boot time, and otherwise throw an exception.

Is there a way to โ€œforceโ€ Django to automatically use the default or auto_now* functions for fixtures? I use manage.py syncdb to create all tables, etc.

I searched google and stack overflow, but could not find the right keywords to search.

UPDATE-1 . The next google group discussion says that objects are saved in raw mode, which means that auto_now* functions auto_now* not taken into account. I'm still trying to check if there is a way to bind some of the model's functions to saving a Django device.

+5
source share
3 answers

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() 

Thus, the model / data is filled in the way you create the model (through the shell, lights, etc.).

+5
source

Automatic loading of source data is not deprecated in Django 1.7 . One solution is the signals you talked about. Another one that I prefer is to create a python script where you will create all the necessary data and execute it in the shell:

 python manage.py shell < create_initial_data.py 
+1
source

I think the problem is when you set default=uuid.uuid4() . There are too many brackets because they mean that you pass the result of uuid.uuid4() to the default argument and not to the function itself, so you have to set default=uuid.uuid4 .

0
source

Source: https://habr.com/ru/post/1202072/


All Articles