[英]Case insensitive field in Django
Currently, I have a Django project containing a model called Event
which has several properties, one of them being full_name
. 目前,我有一个Django项目,其中包含一个名为
Event
的模型,该模型具有多个属性,其中一个属性为full_name
。
from django.db import models
class Event(models.Model):
description = models.CharField(blank=False, max_length=200)
full_name = models.CharField(blank=False, null=True, max_length=200, unique=True)
What I want to acchieve is to prevent users from making two events in which one would be called MyEvent
and the other would be called myevent
, so I want the name to not be case sensitive. 我想要的是阻止用户制作两个事件,其中一个将被称为
MyEvent
而另一个将被称为myevent
,所以我希望该名称不区分大小写。 Furthermore, I come from a country with some funny letters, like š
. 此外,我来自一个有着一些有趣字母的国家,比如
š
。 Users are so used to computer systems not supporting these letters that I want to also prevent the existance of two events, one called šoo
and the other soo
. 用户习惯于不支持这些字母的计算机系统,我也想阻止两个事件的存在,一个叫做
šoo
,另一个叫soo
。
Basically, I have a function myfunction
and want to have a model constraint such that for each instance of the model, the value myfunction(instance.full_name)
is unique. 基本上,我有一个函数
myfunction
并希望有一个模型约束,这样对于模型的每个实例,值myfunction(instance.full_name)
是唯一的。
My first idea, which sort of works, is to have a model form with a clean full name function: 我的第一个想法,即哪种作品,是一个具有干净全名功能的模型表单:
def clean_full_name(self):
return myfunction(self.cleaned_data.get('full_name'))
This works. 这有效。 However, I now have a view in which I want to display the full names of all events, and here, I want to display the original names.
但是,我现在有一个视图,我想在其中显示所有事件的全名,在这里,我想显示原始名称。 Using my approach, this is impossible (the funcion is one-way only).
使用我的方法,这是不可能的(功能只是单向的)。 Is there an elegant solution to this?
有一个优雅的解决方案吗?
You can have another field which is basically the slug of the name. 你可以拥有另一个领域,这个领域基本上就是这个名字的slu .. Infact, I believe it is not a good idea to have the
name
as a unique field (but i should clarify that i do not know your requirement). 事实上,我认为将
name
作为一个独特的领域并不是一个好主意(但我应该澄清,我不知道你的要求)。
Basically, the validation on the slug field ensures uniqueness . 基本上,对slug字段的验证确保了唯一性 。 Also, you can just keep the slug field hidden from all forms, etc..
此外,您可以保持slug字段隐藏所有形式等。
Example: 例:
>>> from django.utils.text import slugify
>>> slugify(u"śtack Overflow")
u'stack-overflow'
>>> slugify(u"stack Overflow")
u'stack-overflow'
>>> slugify(u"stack Overflow")
u'stack-overflow'
>>> slugify(u"stack \t Overflow")
u'stack-overflow'
>>> slugify(u"stack \n Overflow")
u'stack-overflow'
A few of these combinations map to the same slug - which ensures uniqueness for the broad usecases. 其中一些组合映射到相同的slug - 这确保了广泛用例的独特性。
An idea would be to implement a case insensitive unique constraint on the field. 一个想法是在字段上实现不区分大小写的唯一约束。
Possible answers in: 可能的答案:
Just save both full_name and clean_full_name in the database, and make clean_full_name unique. 只需在数据库中保存full_name和clean_full_name,并使clean_full_name唯一。
You can validate the full_name field by putting the validation code in a property setter. 您可以通过将验证代码放在属性设置器中来验证full_name字段。 Have a look at this blog for details.
有关详细信息,请查看此博客 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.