如何在django中创建列表字段 [英] How to create list field in django

查看:102
本文介绍了如何在django中创建列表字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Django(Python)中创建ListField,例如 ListProperty 属性在Google App Engine(Python)中?我的数据是这样的列表: 3,4,5,6,7,8

How do I create a ListField in Django (Python) like the ListProperty property in Google App Engine (Python)? My data is a list like this : 3,4,5,6,7,8.

我必须定义什么属性,如何从中获取值?

What property do I have to define and how would I fetch values from it?

推荐答案

使用 ListField 类型重新访问此类型。但是它会产生一些假设,例如您不在列表中存储复杂类型的事实。因此,我使用 ast.literal_eval()来强制,只有简单的内置类型可以作为成员存储在 ListField

Revisiting this with a ListField type you can use. But it makes a few of assumptions, such as the fact that you're not storing complex types in your list. For this reason I used ast.literal_eval() to enforce that only simple, built-in types can be stored as members in a ListField:

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 Dummy(models.Model):
    mylist = ListField()

采取旋转:

>>> from foo.models import Dummy, ListField
>>> d = Dummy()
>>> d.mylist
[]
>>> d.mylist = [3,4,5,6,7,8]
>>> d.mylist
[3, 4, 5, 6, 7, 8]
>>> f = ListField()
>>> f.get_prep_value(d.numbers)
u'[3, 4, 5, 6, 7, 8]'

你有一个列表作为unicode字符串存储在数据库中,当被退出时,它通过 ast.literal_eval()

There you have it that a list is stored in the database as a unicode string, and when pulled back out it is run through ast.literal_eval().

以前我从这个博客文章中提出了关于 Django中的自定义字段的解决方案

Previously I suggested this solution from this blog post about Custom Fields in Django:


除了CommaSeparatedIntegerField之外,它允许您
存储任何分离的值。您还可以选择指定一个令牌
参数。

An alternative to the CommaSeparatedIntegerField, it allows you to store any separated values. You can also optionally specify a token parameter.



from django.db import models

class SeparatedValuesField(models.TextField):
    __metaclass__ = models.SubfieldBase

    def __init__(self, *args, **kwargs):
        self.token = kwargs.pop('token', ',')
        super(SeparatedValuesField, self).__init__(*args, **kwargs)

    def to_python(self, value):
        if not value: return
        if isinstance(value, list):
            return value
        return value.split(self.token)

    def get_db_prep_value(self, value):
        if not value: return
        assert(isinstance(value, list) or isinstance(value, tuple))
        return self.token.join([unicode(s) for s in value])

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

这篇关于如何在django中创建列表字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆