从数据库中的字段中删除前3个字符

时间:2014-12-30 08:06:25

标签: python django

我的数据库中有医生条目,我想从名字中删除前3个字符。

class Doctor(models.Model):
    name = models.CharField(max_length=1300)
    specialization = models.ForeignKey(Specialization)

    def __unicode__(self):
      return u"%s %s" % (self.name, self.specialization)

    def get_absolute_url(self):
        from django.core.urlresolvers import reverse
        return reverse('k.views.showDocProfile', args=[str(self.id)])

我有很多人参加,所以手动删除并不理想。

1 个答案:

答案 0 :(得分:1)

一次全数据库更改:

for doctor in Doctor.objects.all():
   doctor.name = doctor.name[3:]
   doctor.save()

如果您只需要屏蔽某些用例的名称,则可以在模型中使用属性字段

class Doctor(Model):
   name = CharField(...)

   @property
   def masked_name(self):
       return self.name[3:]

   # To access to property you just use doctor.masked_name on an instance of the Doctor class (it's a property, you don't have to call doctor.masked_name())