views:

98

answers:

1

I see these nice "local flavors" for Canada, but they're only form fields. How would I use them in my model? I can create them all as CharFields sure, but then is there a way to set the default form widget from inside the model so that when I create a ModelForm it'll use them?

+1  A: 

No. Since validation is a form business (and not model business) you can't define that at the model level. You'll have to define a custom form that uses your specific FormField, and use that form.

The docs explain well how to define custom field types and widgets.

BTW: This is an answer I'd love to be proven wrong about... IMO the current state of django's model level validation is not good enough... widgets, as in the way to visually represent the field, are form business, but the fields' validation can be model business as well, like in your case.

Ofri Raviv
The thing is though, we *can* define the default form field, and subsequently widget, for custom model fields: http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#specifying-the-form-field-for-a-model-fieldSo I figure we should be able to override them too. It's perfectly logical in this case, as you mention, because these types have database properties and validation rules which are perfectly suitable for model business, not just rendering.
Mark