在Django中允许Char字段大小写不敏感的使用Lower函数将输入值转换为小写。
代码示例:
from django.db import models
class MyModel(models.Model): name = models.CharField(max_length=100)
from django import forms from .models import MyModel
class MyModelForm(forms.ModelForm): class Meta: model = MyModel fields = ['name']
def clean_name(self):
return self.cleaned_data['name'].lower()
from django.shortcuts import render from .forms import MyModelForm
def my_view(request): if request.method == 'POST': form = MyModelForm(request.POST) if form.is_valid(): # do something pass else: form = MyModelForm() return render(request, 'my_template.html', {'form': form})