django:想要一个用于动态更改序列数据的表单 [英] django: want to have a form for dynamically changed sequence data

查看:207
本文介绍了django:想要一个用于动态更改序列数据的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设我有一些复杂的数据结构,一个任意的例子:



(yaml格式)

  foo:
{
ff :[bar,foobar]
bb:{'some map values'}
}

bar:[str! ,str! ]
foobar:[str! ,str! ]

...



我的目标是允许创建/修改/保存和显示这些数据的Web界面。我无法想象如何定义一种形式和这种数据的模型。问题是数据不是静态的,例如用户可以添加他想要的'ff'值的列表项,即它不是固定两个项'bar'和'foobar',可能有无限数量的添加项目(与其他顺序数据相同)。我只知道'ff'的值是一个列表值。 (我想象网页视图带有一些小的+符号,允许添加数据。)



一旦填写表单,我希望能够使用pyyaml将其转换为yaml并保存数据。并且反向加载文件中的数据并以表单形式显示以允许修改。



所以用两个字 - 如何处理动态的,排序的表单/模型字段。



PS这里还有一个问题是没有内置的类型字段。我想要为每个这样的领域和参考与外键单独的形式。这是正确的方式吗?或者可能会定义自定义字段更好?



非常感谢您提前!!

解决方案

不想为您可以使用的列表创建具体的模型 django-picklefield

  from picklefield.fields import PickledObjectField 

class MyModel(models.Model):
my_list = PickledObjectField(default = [])

然后使用它:

  m1 =我的模型()
m1.my_list = [1,2,3]
m1.save()
m2 = MyModel()
m2.my_list = [a,b,c,d]
m2.save()
/ pre>

更新



在表单中,您应该创建基于您需要的数据类型最简单的是使用文本字段并将逗号分隔的文本转换为列表:

  class MyModelForm(forms.ModelForm): 

class Meta:
model = MyModel

def __init __(self,* args,** kwargs):
super(MyModel,self) __init __(* args,** kwargs)
self.fields [my_list] = forms.CharField(ini​​tial =,。join(self.instance.my_list))

def clean_my_list(self):
data = self.cleaned_data.get('my_list','')
如果数据:
返回data.split(,)#添加错误检查,转换数字等

return []


Django experts - I am a newbie and need your help with the following.

Suppose I have some complicated data structure, an arbitrary example:

(yaml format)

foo: 
{
    ff: [ bar, foobar]
    bb: {'some map values'} 
}

bar: [str!! "", str!! ""]
foobar: [str!! "", str!! ""]

...

My goal is a web interface which allows to create/modify/save and display such data. I can't imagine how to define a form and a model for such kind of data. The problem is that the data is not static, for example the user can add as many list items as he wants for 'ff' value, i.e. it is not fixed two items 'bar' and 'foobar', there may be unlimited number of items added. (The same for the rest of sequenced data). I only know that the value for 'ff' is a list value. (I imagine the web view with some small "+" sign allowing to add data.)

As soon as the form is filled I want to be able to use pyyaml to convert it to yaml and save the data. And the reverse - load the data from a file and display in the form to allow modifications.

So in two words - how to deal with "dynamic, sequenced form/model fields".

P.S. Another problem I have here is having not built-in type fields. I think of having a separate form for each that kind of field and "reference" with Foreign Key. Is that a right way to go? Or may be going with defining custom fields is better?

Many thanks in advance!!

解决方案

If you don't want to create concrete Models for your lists you could use django-picklefield:

from picklefield.fields import PickledObjectField

class MyModel(models.Model):
    my_list = PickledObjectField(default=[])

Then use it like this:

m1 = MyModel()
m1.my_list = [1, 2, 3]
m1.save()
m2 = MyModel()
m2.my_list = ["a", "b", "c", "d"]
m2.save()

UPDATE

In forms you should create custom fields based on the type of data you need. The simplest is to use a text field and convert the comma seperated text into a list:

class MyModelForm(forms.ModelForm):

    class Meta:
        model = MyModel

    def __init__(self, *args, **kwargs):
        super(MyModel, self).__init__(*args, **kwargs)
        self.fields["my_list"] = forms.CharField(initial=",".join(self.instance.my_list))

    def clean_my_list(self):
        data = self.cleaned_data.get('my_list', '')
        if data:
            return data.split(",") # Add error check, convert to number, etc

        return []

这篇关于django:想要一个用于动态更改序列数据的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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