通过 Django 管理站点添加数据时更改大小写(大写/小写)

发布于 2024-07-18 16:43:03 字数 143 浏览 6 评论 0原文

我正在配置我的新项目的管理站点,我有点怀疑我应该怎么做,在通过管理站点添加数据时点击“保存”,所有内容都转换为大写......

编辑:好的我知道 .upper 属性,并且我做了一个视图,我知道该怎么做,但我想知道管理站点上是否有任何可用于字段配置的属性:P

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没︽人懂的悲伤 2024-07-25 16:43:04

你必须覆盖 save()。 文档中的示例:

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()

you have to override save(). An 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()
£冰雨忧蓝° 2024-07-25 16:43:03

如果您的目标是在管理部分中保存时仅将内容转换为大写,那么您需要 创建一个带有自定义验证的表单以更改大小写:

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

如果您的目标是始终将值设为大写,那么您应该 < a href="http://docs.djangoproject.com/en/dev/topics/db/models/#overriding-predefined-model-methods" rel="noreferrer">覆盖模型字段中的保存:

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)

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)
〗斷ホ乔殘χμё〖 2024-07-25 16:43:03

文档中的更新示例建议使用 args、kwargs 来传递:

Django 将不时扩展内置功能
模型方法,添加新参数。 如果你使用 *args, **kwargs
您的方法定义,您保证您的代码将
添加这些参数时自动支持它们。

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()

Updated example from documentation suggests using args, kwargs to pass through as:

Django will, from time to time, extend the capabilities of built-in
model methods, adding new arguments. If you use *args, **kwargs in
your method definitions, you are guaranteed that your code will
automatically support those 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文