Custom model field validation with `clean_fields()`
I've recently read about `clean_fields()` and decided to move some stuff to I that I was doing in my `pre_save` handler previously. My tests failed and then I noticed that neither `clean_fields()` nor `full_clean()` was ever called before saving the model. The documentation says that only `clean()` needs to be explicitly called on the model by the user, so I'm wondering when is `clean_fields` invoked? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: custom model field validation
> So If I create a newforms field then how do I tell the admin to use > that when building the form in the admin? You will need to use the newforms-admin branch to get the admin to use a newforms field ;) Btw: If you just want the regex functionality you can use the RegexField of newforms like so: def formfield(self, **kwargs): defaults = {'regex':r"^[A-Za-z][1-9][1-7][0-9]$"} defaults.update(kwargs) return super(models.CharField, self).formfield(form_class=forms.RegexField, **defaults) Cheers, Jonas --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: custom model field validation
> So If I create a newforms field then how do I tell the admin to use > that when building the form in the admin? If you need this in admin then possibly you have to define your own newforms field with validation. It may be called: myfields.MyField Then you define descednant of CharField from django.db.models like: class MyDBField(models.CharField): def get_internal_type(self): return "CharField def formfield(self, **kwargs): defaults = {'form_class': myfields.MyField} defaults.update(kwargs) return super(MyDBField, self).formfield(**defaults) and you use MyDBField instead of CharField in your models.py file. I've never tried this with Admin, so I'm not sure if this will do what you want, but it is simple enough to check it. -- Maciej Wisniowski --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: custom model field validation
On Feb 7, 2:53 am, Pigletto <[EMAIL PROTECTED]> wrote: > On 7 Lut, 09:58, Mackenzie Kearl <[EMAIL PROTECTED]> > wrote:> I am having trouble finding documentation on how to add custom > > validation to a custom model field. > > > example: > > > class PostalField(models.CharField): > > def __init__(self,*args,**kwargs): > > kwargs['max_length']= 6 > > super(PostalField, self).__init__(*args, **kwargs) > > > def get_internal_type(self): > > return "CharField" > > Seems that you're mixing two types of fields here: fields from > django.db.models and fields from django.newforms.fields. > For this kind of validation (with postal code) you should create > custom newforms field or write custom validator for a form. > This is described at newforms documentation. > > HTH > > -- > Maciej Wisniowski So If I create a newforms field then how do I tell the admin to use that when building the form in the admin? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: custom model field validation
On 7 Lut, 09:58, Mackenzie Kearl <[EMAIL PROTECTED]> wrote: > I am having trouble finding documentation on how to add custom > validation to a custom model field. > > example: > > class PostalField(models.CharField): > def __init__(self,*args,**kwargs): > kwargs['max_length']= 6 > super(PostalField, self).__init__(*args, **kwargs) > > def get_internal_type(self): > return "CharField" > Seems that you're mixing two types of fields here: fields from django.db.models and fields from django.newforms.fields. For this kind of validation (with postal code) you should create custom newforms field or write custom validator for a form. This is described at newforms documentation. HTH -- Maciej Wisniowski --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
custom model field validation
I am having trouble finding documentation on how to add custom validation to a custom model field. example: class PostalField(models.CharField): def __init__(self,*args,**kwargs): kwargs['max_length']= 6 super(PostalField, self).__init__(*args, **kwargs) def get_internal_type(self): return "CharField" now when I save from the admin with this field it only validates like a CharField. How do I fix this to use some function like below. def validate(self): pattern = re.compile("([A-Z,a-z][0-9]){3}") if not pattern.match(value): raise forms.ValidationError('Not A valid Postal Code') --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---