Convert String Uppercase and Lowercase

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Show

class Pessoa(models.Model):
    nome = models.CharField(max_length=60)
    fantasia = models.CharField(max_length=60, blank=True, null=True)
    email = models.EmailField("Endereço de E-Mail", unique=True)

    class Meta:
        abstract = True
    def clean_name(self):
        return self.cleaned_data["nome"].upper()



Save > override method save


class Pessoa(models.Model):
    nome = models.CharField(max_length=60)
    fantasia = models.CharField(max_length=60, blank=True, null=True)
    email = models.EmailField("Endereço de E-Mail", unique=True)
    
    class Meta:
        abstract = True
    
    def save(self, force_insert=False, force_update=False):
        self.nome = self.nome.upper()
        super(Pessoa, self).save(force_insert, force_update)

More like this

  1. Convert CamelCase to lowercase_with_underscores by jdriscoll 5 years, 3 months ago
  2. Roman Numeral Filter by Paperface 6 years, 1 month ago
  3. Filter; Capitalise Sentences (capsentence) by djm 4 years, 3 months ago
  4. use oldforms validators in newforms forms by garywilson 6 years, 1 month ago
  5. Case Insensitive Authentication Backend by ericflo 4 years, 2 months ago

Comments

dudus (on April 15, 2011):

You should use srgs and *kwargs when overriding save mthod. so if new parameters are added in the future it doesn't break.

The clean_name function is only used for Form validation AFAIC. Is it valid inside a Model class?

#

dudus (on April 15, 2011):
  • Fix: You should use *args and **kwargs

#

(Forgotten your password?)