Django复合/嵌套/子表单? [英] Django compound/nested/subforms?

查看:91
本文介绍了Django复合/嵌套/子表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找这些Django SuperForms 的更新版本。似乎无法让它在Django 1.2中工作。特别是,我希望它与ModelForms一起工作。

I'm looking for an updated version of these Django SuperForms. Can't seem to get it to work in Django 1.2. In particular, I'd like it to work with ModelForms.

我的用例与他的几乎相同;我有一个地址模型,我想用作各种地方的子表单。

My use case is almost identical to his; I have an Address model that I'd like to use as a sub-form in various places. It's a pain to try and combine everything in the view func.

推荐答案

我已经更新了superforms.py工作w / 1.2 ,并将其附加到您链接到的机票: http:// code .djangoproject.com / attachment / ticket / 3706 / superform.2.py

I've updated superforms.py to work w/1.2, and attached it to the ticket you linked to: http://code.djangoproject.com/attachment/ticket/3706/superform.2.py

有一个我正在开发的项目可以从中受益,所以我想我会花时间和帮助你。

There's a project I'm working on that could benefit from this, so I figured I'd spend the time and help you out as well.

请记住,我刚刚得到这个工作w / 1.2,并没有真正尝试清理内部物件。现在我有测试用例证明了API,我可以稍后再回去清理。

Keep in mind that I just got this to work w/1.2, and didn't really try to clean up the internals. Now that I have test cases proving the API, I can go back and clean that up later.

如果你使用ModelForms,而且你想要保存()的功能,你必须覆盖SuperForm类的方法。

If you're using it with ModelForms and you'd like the save() functionality you'll have to override the method on your SuperForm class.

我目前在本地在我的常见存储库中有90%的代码覆盖率涵盖多个子方案,混合子方案&声明的表单和ModelForms。我已经在下面列出了测试用例(请注意它使用我的 TestCaseBase 类,但这应该是您的API的要点)。如果您有问题或任何我错过的地方,请告知我。

I currently have it locally in my "common" repository w/~90% code coverage that covers multiple SubForms, mixed SubForms & declared forms, and ModelForms. I've included the test cases below (beware it uses my TestCaseBase class, but this should give you the gist of the API). Let me know if you have questions, or any areas I missed.

from django_common.forms.superform import SuperForm, SubForm
from django_common.test import TestCaseBase
from django import forms

class WhenSuperFormsIsUsedWithOnlySubForms(TestCaseBase):
    def get_superform_with_forms(self, post_data=None):
        class AddressForm(forms.Form):
            street = forms.CharField(max_length=255)
            city = forms.CharField(max_length=255)

        class BusinessLocationForm(forms.Form):
            phone_num = forms.CharField(max_length=255)

        class TestSuperForm(SuperForm):
            address = SubForm(AddressForm)
            business_location = SubForm(BusinessLocationForm)

        return TestSuperForm(data=post_data)

    def should_not_be_valid_with_no_data(self):
        tsf = self.get_superform_with_forms()
        self.assert_false(tsf.is_valid())

    def should_have_two_sub_forms(self):
        tsf = self.get_superform_with_forms()
        self.assert_equal(len(tsf.base_subforms),  2)
        self.assert_equal(len(tsf.forms), 2)

    def should_display_as_ul(self):
        tsf = self.get_superform_with_forms()
        self.assert_equal(tsf.as_ul(), '<li><label for="id_business_location-phone_num">Phone num:</label> <input id="id_business_location-phone_num" type="text" name="business_location-phone_num" maxlength="255" /></li>\n<li><label for="id_address-street">Street:</label> <input id="id_address-street" type="text" name="address-street" maxlength="255" /></li>\n<li><label for="id_address-city">City:</label> <input id="id_address-city" type="text" name="address-city" maxlength="255" /></li>')

    def should_display_as_table(self):
        tsf = self.get_superform_with_forms()
        self.assert_equal(tsf.as_table(), '<tr><th><label for="id_business_location-phone_num">Phone num:</label></th><td><input id="id_business_location-phone_num" type="text" name="business_location-phone_num" maxlength="255" /></td></tr>\n<tr><th><label for="id_address-street">Street:</label></th><td><input id="id_address-street" type="text" name="address-street" maxlength="255" /></td></tr>\n<tr><th><label for="id_address-city">City:</label></th><td><input id="id_address-city" type="text" name="address-city" maxlength="255" /></td></tr>')

    def should_display_as_p(self):
        tsf = self.get_superform_with_forms()
        self.assert_equal(tsf.as_p(), '<p><label for="id_business_location-phone_num">Phone num:</label> <input id="id_business_location-phone_num" type="text" name="business_location-phone_num" maxlength="255" /></p>\n<p><label for="id_address-street">Street:</label> <input id="id_address-street" type="text" name="address-street" maxlength="255" /></p>\n<p><label for="id_address-city">City:</label> <input id="id_address-city" type="text" name="address-city" maxlength="255" /></p>')

    def should_display_as_table_with_unicode(self):
        tsf = self.get_superform_with_forms()
        self.assert_equal(tsf.__unicode__(), tsf.as_table())

    def should_be_valid_if_good_data(self):
        data = {
            'business_location-phone_num' : '8055551234',
            'address-street' : '1234 Street Dr.',
            'address-city' : 'Santa Barbara',
        }
        tsf = self.get_superform_with_forms(data)
        self.assert_true(tsf.is_valid())
        self.assert_equal(tsf.cleaned_data['business_location']['phone_num'],
                          '8055551234')
        self.assert_equal(tsf.cleaned_data['address']['street'], '1234 Street Dr.')
        self.assert_equal(tsf.cleaned_data['address']['city'], 'Santa Barbara')

    def should_be_invalid_if_missing_data(self):
        data = {
            'business_location-phone_num' : '8055551234',
            'address-street' : '1234 Street Dr.',
        }
        tsf = self.get_superform_with_forms(data)
        self.assert_false(tsf.is_valid())

        self.assert_false(tsf.errors['business_location'])
        self.assert_true(tsf.errors['address'])
        self.assert_equal(tsf.errors['address']['city'], ['This field is required.'])

    def should_be_invalid_if_invalid_data(self):
        data = {
            'business_location-phone_num' : '8055551234',
            'address-street' : '1234 Street Dr.',
            'address-city' : '',
        }
        tsf = self.get_superform_with_forms(data)
        self.assert_false(tsf.is_valid())


class WhenSuperformsIsUsedWithSubFormsAndDeclaredFields(TestCaseBase):
    """Some basic sanity checks that working with fields combined with SubForms works."""
    def get_superform_with_forms(self, post_data=None):
        class AddressForm(forms.Form):
            street = forms.CharField(max_length=255)

        class TestSuperForm(SuperForm):
            name = forms.CharField(max_length=255)
            address = SubForm(AddressForm)

        return TestSuperForm(data=post_data)

    def should_not_be_valid_with_no_data(self):
        tsf = self.get_superform_with_forms()
        self.assert_false(tsf.is_valid())

    def should_have_two_forms_and_a_single_subform(self):
        tsf = self.get_superform_with_forms()
        self.assert_equal(len(tsf.base_subforms),  1)
        self.assert_equal(len(tsf.forms), 2)

    def should_print_as_table(self):
        tsf = self.get_superform_with_forms()
        self.assert_equal(tsf.as_table(), '<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="255" /></td></tr>\n<tr><th><label for="id_address-street">Street:</label></th><td><input id="id_address-street" type="text" name="address-street" maxlength="255" /></td></tr>')

    def should_validate_when_fields_exist(self):
        data = {
            'name': 'Sam',
            'address-street': 'Some Street',
        }
        tsf = self.get_superform_with_forms(data)
        self.assert_true(tsf.is_valid())

        self.assert_equal(tsf.cleaned_data['name'], 'Sam')
        self.assert_equal(tsf.cleaned_data['address']['street'], 'Some Street')

    def should_not_validate_with_invalid_data(self):
        data = {
            'name': '',
            'address-street': 'Some Street',
        }
        tsf = self.get_superform_with_forms(data)
        self.assert_false(tsf.is_valid())

        self.assert_equal(tsf.errors['name'], ['This field is required.'])



class WhenSuperformsIsUsedWithModelForms(TestCaseBase):
    def get_superform_with_forms(self, post_data=None):
        from django.db import models
        class Address(models.Model):
            city = models.CharField(max_length=255)

        class AddressForm(forms.ModelForm):
            class Meta:
                model = Address

        class TestSuperForm(SuperForm):
            address = SubForm(AddressForm)

        return TestSuperForm(data=post_data)

    def should_not_be_valid_with_no_data(self):
        tsf = self.get_superform_with_forms()
        self.assert_false(tsf.is_valid())

    def should_have_two_forms_and_a_single_subform(self):
        tsf = self.get_superform_with_forms()
        self.assert_equal(len(tsf.base_subforms),  1)
        self.assert_equal(len(tsf.forms), 1)

    def should_print_as_table(self):
        tsf = self.get_superform_with_forms()
        self.assert_equal(tsf.as_table(), '<tr><th><label for="id_address-city">City:</label></th><td><input id="id_address-city" type="text" name="address-city" maxlength="255" /></td></tr>')

    def should_validate_when_fields_exist(self):
        data = {
            'address-city': 'Some City',
        }
        tsf = self.get_superform_with_forms(data)
        self.assert_true(tsf.is_valid())

        self.assert_equal(tsf.cleaned_data['address']['city'], 'Some City')

    def should_not_validate_with_invalid_data(self):
        data = {
            'address-city': '',
        }
        tsf = self.get_superform_with_forms(data)
        self.assert_false(tsf.is_valid())

        self.assert_equal(tsf.errors['address']['city'], ['This field is required.'])

享受!

这篇关于Django复合/嵌套/子表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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