自定义Django模型字段以存储多个值。
django3-collectionfield的Python项目详细描述
用于存储集合的可重用Django模型字段。为django3和python3移植。 你可以在这里找到原件:https://github.com/escer/django-collectionfield
特点
- 高度可配置的model field(更改集合和项类型、排序、选项、项唯一性等)
- custom lookups以简化对集合项的查询
- form fields用于处理集合
- 集合item validators
- 扩展的get_FIELD_display方法,用于有选择的模型字段
- 与不支持多值列的数据库后端一起工作(如用于PostgreSQL的ArrayField)
安装
pipinstalldjango3-collectionfield
使用
模型场
使用存储字符串列表的字段定义模型:
^{pr2}$将值传递到模型字段:
my_model=MyModel.objects.create(tags=['test','values'])my_model.values['test','values']
进行查询
检索集合中具有特定值的模型实例:
MyModel.objects.filter(tags__has='test')
检索集合中存在ALL值的模型实例(忽略项的顺序):
MyModel.objects.filter(tags__hasall=['test','values'])
检索集合中存在的ANY值的模型实例:
MyModel.objects.filter(tags__hasany=['test','values'])
定制系列
自定义集合和项类型:
classIntegerSet(models.Model):# This field will provide sets of integers# instead of default lists of strings:values=CollectionField(collection_type=set,item_type=int)
排序和唯一性:
classSortedUniqueTextList(models.Model):# Before saving, items will be sorted and duplicates dropped:texts=CollectionField(sort=True,unique_items=True)
选择和集合大小限制:
classTaggedModel(models.Model):tags=CollectionField(# Both choices and max_items limit are checked during model validation.choices=(('action',"Action"),('comedy',"Comedy"),('horror',"Horror"),# ...),max_items=2)
get_FIELD_display方法可以处理多个选项,并提供自定义显示的选项:
tagged_model=TaggedModel.objects.create(tags=['action','horror'])tagged_model.get_tags_display()"Action, Horror"defli_mapper(value,label):return"<li>{0}</li>".format(label)deful_wrapper(field_display):return"<ul>{0}</ul>".format(field_display)tagged_model.get_tags_display(delimiter='',mapper=li_mapper,wrapper=ul_wrapper)'<ul><li>Action</li><li>Horror</li></ul>'
Django内置验证器处理整个字段值。django3-collectionfield提供单个集合项的验证:
fromcollectionfield.validatorsimport(ItemMinValueValidator,ItemMaxValueValidator)classIntegerList(models.Model):values=CollectionField(item_type=int,# item validators check each item separately:validators=[ItemMinValueValidator(1),ItemMaxValueValidator(5)])
表单域
django-collectionfield附带2个表单字段:
fromcollectionfield.formsimportCollectionField,CollectionChoiceField# ``collectionfield.forms.CollectionField`` converts comma-separated text# into collection of values:classMyForm(forms.Form):values=CollectionField()my_form=MyForm({'values':"A, B, C"})my_form.is_valid()Truemy_form.cleaned_data['values']['A','B','C']# ``collectionfield.forms.CollectionChoiceField`` behaves more like# regular MultipleChoiceField:classMyChoiceForm(forms.Form):values=CollectionChoiceField(choices=(('action',"Action"),('comedy',"Comedy"),('horror',"Horror"),# ...))my_choice_form=MyChoiceForm({'values':['action','comedy']})my_choice_form.is_valid()Truemy_choice_form.cleaned_data['values']['action','comedy']
两个表单字段支持与模型字段相同的参数集:
fromcollectionfield.formsimportCollectionFieldclassMyForm(forms.Form):values=CollectionField(collection_type=set,item_type=int)my_form=MyForm({'values':"1, 2, 1, 3"})my_form.is_valid()Truemy_form.cleaned_data['values']{1,2,3}
在数据库中的表示
CollectionField使用以下格式将其值转换为最多1024个字符的字符串:
"|item1|item2|item3|"
可以配置默认分隔符(“|”)和最大长度:
classMyModel(models.Model):values=CollectionField(delimiter="$",max_length=2000)
要求
Python:3.5
詹戈:3
- 项目
标签: