gpt4 book ai didi

python - Django fixtures 以默认值保存

转载 作者:太空狗 更新时间:2023-10-30 01:01:28 25 4
gpt4 key购买 nike

我使用的是 Django 1.7,但我的设备有问题。

我希望 Django 使用默认值或使用 save() 方法来创建未指定的值。

这是我当前的对象:

# 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

如您所见,当我生成一个新的 Post() 时,created_atupdated_atuuid 值是在 save() 上自动创建的。但是当我使用 fixtures 时,出现以下错误:

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

如果我在我的 fixture 文件中指定了一个 uuid,那么我会在 created_atupdated_at 上收到错误。所以我必须指定每个字段的内容,即使我希望它是“自动的”。

来自documentation (为什么这在 django admin 文档中?!),我知道 save() 方法没有被调用,所以这就是为什么我将所有内容都放入 save() 方法不起作用。但是不应该启用/使用 defaultauto_now* 功能吗?

When fixture files are processed, the data is saved to the database as is. Model defined save() methods are not called, and any pre_save or post_save signals will be called with raw=True since the instance only contains attributes that are local to the model. You may, for example, want to disable handlers that access related fields that aren’t present during fixture loading and would otherwise raise an exception

有没有办法“强制”Django 自动使用 defaultauto_now* fixtures 特性?我正在使用 manage.py syncdb 创建所有表等。

我在 google 和 stack overflow 上进行了搜索,但似乎找不到合适的搜索关键字。

UPDATE-1:以下 google group discussion表示对象以 raw 模式保存,这意味着不考虑 auto_now* 功能。我仍在寻找是否有办法将某些模型函数挂接到 Django fixture 保存。

最佳答案

解决方案是使用 django 信号:

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

这样,无论您以何种方式创建模型(通过 shell、固定装置等),模型/数据都会被填充。

关于python - Django fixtures 以默认值保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25724318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com