Задать вопрос

Тел: +7 965 3737 888

472

Просмотров

2

Ответов

StringListField (database field)

This django.db.models.Field stores a list of python strings in one database column.

Вопрос полезен? Да0/Нет0
file_4192.py(1.3Кб)
None

Ответы (2):

Ответfylb:30.08.2012
Ответ полезен? Да0/Нет0

When using choices, in order to be able to use the widget, I had to add a validate method:

def validate(self, value, model_instance):
    """
    Validates value and throws ValidationError. Subclasses should override
    this to provide validation logic.
    """
    if not self.editable:
        # Skip validation for non-editable fields.
        return
    if self._choices and value:
        l = value
        if type(value) != list:
            l = [ value ]
        for v in value:
            for option_key, option_value in self.choices:
                if isinstance(option_value, (list, tuple)):
                    # This is an optgroup, so look inside the group for options.
                    for optgroup_key, optgroup_value in option_value:
                        if v == optgroup_key:
                            return
                elif v == option_key:
                    return
            raise exceptions.ValidationError(self.error_messages['invalid_choice'] % v)

    if value is None and not self.null:
        raise exceptions.ValidationError(self.error_messages['null'])

    if not self.blank and value in validators.EMPTY_VALUES:
        raise exceptions.ValidationError(self.error_messages['blank'])

Ответdharris:28.10.2009
Ответ полезен? Да0/Нет0

I've modified this locally to handle saving and loading empty values better.

In get_db_prep_save (line 37-38), I added:

if not value:
    return None

without this, an empty string was saved -- I prefer a NULL in the db.

Line 26-27, I changed this to

if value is None or value == '':
    return []