Django的文檔不能解釋如何使用MultiValueField和MultiWidget。我試過解剖one implementation,並沒有很好的結果。有人會介意給我一個正確方向的快速指示嗎?Django:MultiValueField和MultiWidget
我的例子:
widgets.py
from django import forms
class TestMultiWidget(forms.MultiWidget):
def __init__(self, attrs=None):
widgets = (
forms.TextInput(attrs=attrs),
forms.TextInput(attrs=attrs),
)
super(TestMultiWidget, self).__init__(widgets, attrs)
def decompress(self, value):
if value:
return value.split(':::')[0:2]
return ['', '']
fields.py
from django import forms
from widgets import TestMultiWidget
class TestMultiField(forms.MultiValueField):
widget = TestMultiWidget
def __init__(self, *args, **kwargs):
fields = (
forms.CharField(),
forms.CharField(),
)
super(TestMultiField, self).__init__(fields, *args, **kwargs)
def compress(self, data_list):
if data_list:
return ':::'.join(data_list)
return ''
models.py
from django.db import models
from util.fields import TestMultiField
class Test(models.Model):
a = models.CharField(max_length=128)
b = TestMultiField()
c = models.CharField(max_length=128)
admin.py
from django.contrib import admin
from models import Test
admin.site.register(Test)
任何人都有線索這裏發生了什麼?我的猜測是,有一些意外的異常抑制發生,但我一直無法找到源。
謝謝!
MultiWidget HOWTO:http://stackoverflow.com/questions/3511855/how-do-我使用的,Django的-multiwidget/4663801#4663801 – Udi 2011-06-15 00:13:55