class HayBaleForm(forms.ModelForm): weight = QuantityFormField(base_units='gram', unit_choices=['ounce', 'gram']) class Meta: model = HayBale exclude = []
class BoxModelForm(ModelForm): new_weight = QuantityFormField(base_units="gram", unit_choices=["gram", "ounce", "milligram"]) class Meta: model = Box exclude = []
class MegalithForm(forms.ModelForm): weight = QuantityFormField( base_units='tonnes', unit_choices=['tonnes', 'ounces', 'grams', 'kilograms']) class Meta: model = Megalith fields = '__all__'
class SimpleForm(Form): hello = QuantityFormField(base_units="kilogram", required=False)
def test_unit_choices_must_match_base_dimensionality(self): with self.assertRaises(DimensionalityError): field = QuantityFormField(base_units='gram', unit_choices=['meter', 'ounces'])
def test_unit_choices_must_be_valid_units(self): with self.assertRaises(UndefinedUnitError): field = QuantityFormField(base_units="mile", unit_choices=['gunzu'])
def test_base_units_is_included_by_default(self): field = QuantityFormField(base_units="mile", unit_choices=['meters', 'feet']) self.assertIn('mile', field.units)
def test_base_units_is_required_for_form_field(self): with self.assertRaises(ValueError): field = QuantityFormField()
class UnitChoicesForm(forms.Form): distance = QuantityFormField( base_units='kilometer', unit_choices=['mile', 'kilometer', 'yard', 'feet'])
class NullableWeightForm(forms.Form): weight = QuantityFormField(base_units='gram', required=False)
class TestForm(forms.Form): """docstring for TestForm""" amount = QuantityFormField(base_units="grams", unit_choices=['ounce', 'gram'])