Django:MultiValueField 和 MultiWidget

发布于 2024-11-08 05:42:37 字数 1760 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2024-11-15 05:42:37

请注意 django.forms.MultiValueField 是一个 表单 字段 而不是 model 字段(如 django.db.models.CharField)。因此,它不会被视为测试模型中的模型字段,也不会在数据库中创建。 (您可以使用./manage.py sqlall myapp进行检查)。

将 models.py 更改为:

from django.db import models
from fields import TestMultiField

class TestMultiModelField(models.Field):

    def formfield(self, **kwargs):
        defaults = {'form_class': TestMultiField}
        defaults.update(kwargs)
        return super(TestMultiModelField, self).formfield(**defaults)

    def get_internal_type(self):
        return 'TextField'        

class Test(models.Model):
    a = models.CharField(max_length=128)
    b = TestMultiModelField()
    c = models.CharField(max_length=128)      

删除表(在 linux/mac 上:./manage.py sqlclear myapp | ./manage.py dbshel​​l)并使用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:

from django.db import models
from fields import TestMultiField

class TestMultiModelField(models.Field):

    def formfield(self, **kwargs):
        defaults = {'form_class': TestMultiField}
        defaults.update(kwargs)
        return super(TestMultiModelField, self).formfield(**defaults)

    def get_internal_type(self):
        return 'TextField'        

class Test(models.Model):
    a = models.CharField(max_length=128)
    b = TestMultiModelField()
    c = models.CharField(max_length=128)      

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文