Django 模型字段默认基于同一模型中的另一个字段 [英] Django Model Field Default Based Off Another Field in Same Model

查看:44
本文介绍了Django 模型字段默认基于同一模型中的另一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,我想包含一个主题名称和他们的姓名首字母(他的数据有些匿名并由首字母跟踪).

I have a model that I would like to contain a subjects name and their initials (he data is somewhat anonymized and tracked by initials).

现在,我写了

class Subject(models.Model):

    name = models.CharField("Name", max_length=30)
    def subject_initials(self):
        return ''.join(map(lambda x: '' if len(x)==0 else x[0],
                           self.name.split(' ')))
    # Next line is what I want to do (or something equivalent), but doesn't work with
    # NameError: name 'self' is not defined
    subject_init = models.CharField("Subject Initials", max_length=5, default=self.subject_initials)

如最后一行所示,我希望能够将首字母作为字段(独立于名称)实际存储在数据库中,但使用基于名称字段的默认值进行初始化.但是,我遇到了问题,因为 django 模型似乎没有自我".

As indicated by the last line, I would prefer to be able to have the initials actually get stored in the database as a field (independent of name), but that is initialized with a default value based on the name field. However, I am having issues as django models don't seem to have a 'self'.

如果我将行更改为 subject_init = models.CharField("Subject initials", max_length=2, default=subject_initials),我可以执行同步数据库,但不能创建新主题.

If I change the line to subject_init = models.CharField("Subject initials", max_length=2, default=subject_initials), I can do the syncdb, but can't create new subjects.

这在 Django 中是否可行,有一个可调用的函数会根据另一个字段的值为某个字段提供默认值?

Is this possible in Django, having a callable function give a default to some field based on the value of another field?

(出于好奇,我想将我的商店首字母分开的原因是在极少数情况下奇怪的姓氏可能与我正在跟踪的姓氏不同.例如,其他人决定将主题 1 命名为John O"Mallory"的首字母是JM"而不是JO",并希望以管理员身份修复它.)

(For the curious, the reason I want to separate my store initials separately is in rare cases where weird last names may have different than the ones I am tracking. E.g., someone else decided that Subject 1 Named "John O'Mallory" initials are "JM" rather than "JO" and wants to fix edit it as an administrator.)

推荐答案

模型当然有自我"!只是您试图将模型类的属性定义为依赖于模型实例;这是不可能的,因为在您定义类及其属性之前,实例不(也不能)存在.

Models certainly do have a "self"! It's just that you're trying to define an attribute of a model class as being dependent upon a model instance; that's not possible, as the instance does not (and cannot) exist before your define the class and its attributes.

要得到你想要的效果,重写模型类的save()方法.对实例进行必要的更改,然后调用超类的方法进行实际保存.这是一个简单的例子.

To get the effect you want, override the save() method of the model class. Make any changes you want to the instance necessary, then call the superclass's method to do the actual saving. Here's a quick example.

def save(self, *args, **kwargs):
    if not self.subject_init:
        self.subject_init = self.subject_initials()
    super(Subject, self).save(*args, **kwargs)

这在覆盖模型方法 在文档中.

这篇关于Django 模型字段默认基于同一模型中的另一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆