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

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

问题描述

我有一个模型,我想包含一个主题名称和他们的缩写。 (数据有点匿名,并以缩写填写)。



现在,我写了

 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
b b b b b b b b b b b b b b b b b b b b b b b b b b b b return''.join(map(lambda x:''if len(x)== 0 else x [0],
self.name.split(''))
#下一行是我想要做的(或一些相当的东西),但是不能使用
#NameError:name'self'没有定义
subject_init = models.CharField(Subject Initials,max_length = 5, default = self.subject_initials)

如最后一行所示,我更愿意拥有首字母实际上作为字段存储在数据库中(与名称无关),但是使用名称字段的默认值进行初始化。然而,我有问题,因为django模型似乎没有自我。



如果我将行更改为 subject_init = models.CharField(Subject initials,max_length = 2,default = subject_initials),我可以做syncdb,但不能创建新的主题。



这是可能在django中,具有可调用函数给默认的一些字段对于另一个字段的价值?



(为了好奇,我想分开我的商店首字母的原因是在极少数情况下,奇怪的姓氏可能不同于例如,有人决定将主题1命名为John O'Mallory的缩写为JM而不是JO,并希望将其修改为管理员。)

解决方案

模型肯定有一个自我!只是你试图将模型类的属性定义为依赖于模型实例;这是不可能的,因为在定义类和其属性之前,实例不存在(而不能)存在。



要获得所需的效果,请覆盖save()模型类的方法。对所需的实例进行任何更改,然后调用超类的方法来进行实际的保存。这是一个简单的例子。

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

这被覆盖在覆盖模型方法在文档中。


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

Right now, I wrote

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)

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'.

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.

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

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

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)

This is covered in Overriding Model Methods in the documentation.

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

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