The Django documentation does not provide a very detailed explanation of how to use MultiValueField and MultiWidget. I tried to analyze one implementation and did not have good results. Will someone mind give me a quick pointer in the right direction?
My example:
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)
And the administrator received .
Does anyone know what is going on here? I assume there is some unintended exception exception, but I could not find the source.
Thanks!
source share