Login

Convert String Uppercase and Lowercase

Author:
oraculum
Posted:
April 15, 2011
Language:
Python
Version:
1.2
Score:
0 (after 2 ratings)

To convert string to lower case replace upper() for lower()

 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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 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

#

Please login first before commenting.