通过Django管理站点添加数据时,更改大小写(上/下) [英] Changing case (upper/lower) on adding data through Django admin site

查看:194
本文介绍了通过Django管理站点添加数据时,更改大小写(上/下)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在配置我的新项目的管理员网站,我有一点疑问我应该怎么做,当通过管理员网站添加数据时点击保存,一切都转换为大写...



编辑:Ok我知道.upper属性,而我做了一个视图,我会知道如何做,但我想知道是否有任何可用的属性对于管理站点上的现场配置:P

解决方案

如果您的目标是只有在保存时将东西转换为大写管理部分,您将需要创建一个具有自定义验证的表单以使案例更改:

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

如果您的目标是始终使用大写的值,那么您应该覆盖保存在模型字段中:

  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)


I'm configuring the admin site of my new project, and I have a little doubt on how should I do for, on hitting 'Save' when adding data through the admin site, everything is converted to upper case...

Edit: Ok I know the .upper property, and I I did a view, I would know how to do it, but I'm wondering if there is any property available for the field configuration on the admin site :P

解决方案

If your goal is to only have things converted to upper case when saving in the admin section, you'll want to create a form with custom validation to make the case change:

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

If your goal is to always have the value in uppercase, then you should override 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)

这篇关于通过Django管理站点添加数据时,更改大小写(上/下)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆