欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

django自定义Field实现一个字段存储以逗号分隔的字符串

程序员文章站 2022-05-31 15:06:59
实现了在一个字段存储以逗号分隔的字符串,返回一个相应的列表 复制代码 代码如下:from django import formsfrom django.db import...

实现了在一个字段存储以逗号分隔的字符串,返回一个相应的列表

复制代码 代码如下:

from django import forms
from django.db import models
from django.utils.text import capfirst
from django.core import exceptions


class multiselectformfield(forms.multiplechoicefield):
    widget = forms.checkboxselectmultiple

    def __init__(self, *args, **kwargs):
        self.max_choices = kwargs.pop('max_choices', 0)
        super(multiselectformfield, self).__init__(*args, **kwargs)

    def clean(self, value):
        if not value and self.required:
            raise forms.validationerror(self.error_messages['required'])
        # if value and self.max_choices and len(value) > self.max_choices:
        #     raise forms.validationerror('you must select a maximum of %s choice%s.'
        #             % (apnumber(self.max_choices), pluralize(self.max_choices)))
        return value


class multiselectfield(models.field):
    __metaclass__ = models.subfieldbase

    def get_internal_type(self):
        return "charfield"

    def get_choices_default(self):
        return self.get_choices(include_blank=false)

    def _get_field_display(self, field):
        value = getattr(self, field.attname)
        choicedict = dict(field.choices)

    def formfield(self, **kwargs):
        # don't call super, as that overrides default widget if it has choices
        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name),
                    'help_text': self.help_text, 'choices': self.choices}
        if self.has_default():
            defaults['initial'] = self.get_default()
        defaults.update(kwargs)
        return multiselectformfield(**defaults)

    def get_prep_value(self, value):
        return value

    def get_db_prep_value(self, value, connection=none, prepared=false):
        if isinstance(value, basestring):
            return value
        elif isinstance(value, list):
            return ",".join(value)

    def to_python(self, value):
        if value is not none:
            return value if isinstance(value, list) else value.split(',')
        return ''

    def contribute_to_class(self, cls, name):
        super(multiselectfield, self).contribute_to_class(cls, name)
        if self.choices:
            func = lambda self, fieldname = name, choicedict = dict(self.choices): ",".join([choicedict.get(value, value) for value in getattr(self, fieldname)])
            setattr(cls, 'get_%s_display' % self.name, func)

    def validate(self, value, model_instance):
        arr_choices = self.get_choices_selected(self.get_choices_default())
        for opt_select in value:
            if (int(opt_select) not in arr_choices):  # the int() here is for comparing with integer choices
                raise exceptions.validationerror(self.error_messages['invalid_choice'] % value)
        return

    def get_choices_selected(self, arr_choices=''):
        if not arr_choices:
            return false
        list = []
        for choice_selected in arr_choices:
            list.append(choice_selected[0])
        return list

    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return self.get_db_prep_value(value)