Change case (upper / lower) when adding data through the Django admin site

I am setting up the admin site for my new project, and I have a little doubt about how to do this, when I click "Save" when adding data through the admin site, everything is converted to uppercase ...

Edit: Good. I know the .upper property, and I made a presentation, I would know how to do it, but I'm wondering if there is any property for setting the field on the admin site: P

+4
source share
3 answers

If your goal is only to make things uppercase when stored in the admin section, you'll want to create a custom validation form to change case:

class MyArticleAdminForm(forms.ModelForm): class Meta: model = Article def clean_name(self): return self.cleaned_data["name"].upper() 

If your goal is always to be uppercase, you must override the save in the model field:

 class Blog(models.Model): name = models.CharField(max_length=100) def save(self, force_insert=False, force_update=False): self.name = self.name.upper() super(Blog, self).save(force_insert, force_update) 
+13
source

An updated example from the documentation suggests using args, kwargs to go through:

Django will expand the capabilities of the embedded model from time to time by adding new arguments. If you use * args, ** kwargs in your method definitions, you are guaranteed that your code will automatically support these arguments when they are added.

 class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def save(self, *args, **kwargs): do_something() super(Blog, self).save( *args, **kwargs) # Call the "real" save() method. do_something_else() 
+3
source

you must override save () . Example from the documentation:

 class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def save(self, force_insert=False, force_update=False): do_something() super(Blog, self).save(force_insert, force_update) # Call the "real" save() method. do_something_else() 
+1
source

All Articles