最佳:django清洁或postgres功能/触发器

时间:2014-04-02 06:25:03

标签: django postgresql django-admin postgresql-9.1

我有以下型号。

class Customer(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField(blank=True)
    address = models.TextField()
    city = models.CharField(max_length=25, blank=True)
    state = models.CharField(max_length=3, choices=STATE_CHOICES, blank=True)
    pincode = models.CharField(max_length=8, blank=True)
    district = models.CharField(max_length=25, blank=True)

我希望first_name, last_name, city and district的第一个字母为capitalised,其余为lower case。 哪个过程更加优化,在模型的干净方法中执行

def clean():
    for field_name in ['first_name', 'last_name', 'city', 'district',]:
        val = getattr(self, field_name, False)
        if val:
            setattr(self, field_name, val.strip().title())

或使用initcap作为数据库中的触发器。 我使用postgres 9.1。

0 个答案:

没有答案