Django:MultiValueField 和 MultiWidget
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)
和 生成的管理员。
有人知道这里发生了什么吗?我的猜测是发生了一些意外的异常抑制,但我无法找到源头。
谢谢!
Django's documentation doesn't do a very thorough job of explaining how to use MultiValueField and MultiWidget. I've tried dissecting the one implementation and haven't had good results. Would someone mind giving 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 resulting admin.
Anybody have a clue what's happening here? My guess is that there's some unintended exception suppression happening, but I haven't been able to locate the source.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意 django.forms.MultiValueField 是一个 表单 字段 而不是 model 字段(如 django.db.models.CharField)。因此,它不会被视为测试模型中的模型字段,也不会在数据库中创建。 (您可以使用
./manage.py sqlall myapp
进行检查)。将 models.py 更改为:
删除表(在 linux/mac 上:
./manage.py sqlclear myapp | ./manage.py dbshell
)并使用syncdb 创建表,这次使用 b 列。立即检查您的管理员。解释:
要创建自定义模型字段,请按照以下步骤操作:https://docs。 djangoproject.com/en/dev/howto/custom-model-fields/
设置 model 字段的匹配 form 字段,formfield 方法。
(顺便说一句,设计模型字段的“正确”方法可能有点不同,使用 to_python 和 get_prep_value)
Please notice that django.forms.MultiValueField is a form field and not a model field (like django.db.models.CharField). Therefore, it is not treated as a model field in your Test model, and was not created in your database. (You can check it with
./manage.py sqlall myapp
).Change your models.py to:
drop your table (on linux/mac:
./manage.py sqlclear myapp | ./manage.py dbshell
) and syncdb to create your table, this time with column b. Check your admin now.Explanation:
To create a custom model field, follow this: https://docs.djangoproject.com/en/dev/howto/custom-model-fields/
The set the model field's matching form field, the formfield method was used.
(BTW, The "correct" way to design the model field is probably a bit different, using to_python and get_prep_value)