from django.db import models
import ast
class ListField(models.TextField):
__metaclass__ = models.SubfieldBase
description = "Stores a python list"
def __init__(self, *args, **kwargs):
super(ListField, self).__init__(*args, **kwargs)
def to_python(self, value):
if not value:
value = []
if isinstance(value, list):
return value
return ast.literal_eval(value)
def get_prep_value(self, value):
if value is None:
return value
return unicode(value)
def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
return self.get_db_prep_value(value)
class ListModel(models.Model):
test_list = ListField()
import sys
...
def get_prep_value(self, value):
if value is None:
return value
if sys.version_info[0] >= 3:
if isinstance(out_data, type(b'')):
return value.decode(encoding='utf-8', errors='ignore')
else:
if isinstance(out_data, type(b'')):
return unicode(value, encoding='utf-8', errors='ignore')
return str(value)
...
这是一个相当老的主题,但是由于在搜索“ Django list field”时返回了它,因此我将共享我修改用于 Python 3和 Django 2的自定义 Django list field 代码。它现在支持管理界面,不使用 eval (这是 Prashant Gaur 代码中的一个巨大安全漏洞)。
from django.db import models
from typing import Iterable
class ListField(models.TextField):
"""
A custom Django field to represent lists as comma separated strings
"""
def __init__(self, *args, **kwargs):
self.token = kwargs.pop('token', ',')
super().__init__(*args, **kwargs)
def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
kwargs['token'] = self.token
return name, path, args, kwargs
def to_python(self, value):
class SubList(list):
def __init__(self, token, *args):
self.token = token
super().__init__(*args)
def __str__(self):
return self.token.join(self)
if isinstance(value, list):
return value
if value is None:
return SubList(self.token)
return SubList(self.token, value.split(self.token))
def from_db_value(self, value, expression, connection):
return self.to_python(value)
def get_prep_value(self, value):
if not value:
return
assert(isinstance(value, Iterable))
return self.token.join(value)
def value_to_string(self, obj):
value = self.value_from_object(obj)
return self.get_prep_value(value)