Re: Custom Validation on Inline Generic Foreign Key
I went ahead and hardcoded checks for fields that as of today I know will only be in one of the models. I would still love to hear any cleaner solutions if anyone has them. On Aug 26, 10:12 am, Johnwrote: > Any ideas? > > In the inline model's validation I could hardcode checks for data that > I expect will only be in one of the models, but that does not seem > like the best option. I am still hoping for a simpler alternative to > this. > > Thanks. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Custom Validation on Inline Generic Foreign Key
Any ideas? In the inline model's validation I could hardcode checks for data that I expect will only be in one of the models, but that does not seem like the best option. I am still hoping for a simpler alternative to this. Thanks. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Custom validation in model
Overriding the clean method of the modelform is the way to go. If you're getting an error about not having 'cleaned_data,' it could be because your custom clean method doesn't return anything -- you're responsible for returning it if you override it that method. Shawn -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Custom validation in model formset
try this... from django.forms import ValidationError ... for form in self.forms: ... raise ValidationError('whatever') On Oct 11, 1:32 pm, Duane Hiltonwrote: > Changing forms to form brings a new error: > > *Exception Value: 'CandidateFeedForm' object has no attribute > 'ValidationError'* > > Below is the view that is using the BaseCandFormSet code that I included in > the original message. Maybe something is wrong in it? Thanks in advance for > your suggestions. I've spent a few days using the documentation and trying > to get this to work. I really appreciate the help. ... > > *def state_office(request): > CandFormSet = modelformset_factory(CandidateFeed, > **queryset=CandidateFeed.objects.all().order_by('last_name', > 'county'),** formset=BaseCandFormSet, extra=0) > if request.method == 'POST': > formset = CandFormSet(request.POST, request.FILES) > if formset.is_valid(): > formset.save() > return HttpResponseRedirect('/foo/bar/') > else: > formset = > CandFormSet(queryset=CandidateFeed.objects.all().order_by('last_name', > 'county')) > return render_to_response('state_office.html', { 'formset': formset })* -- 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 validation in model formset
Changing forms to form brings a new error: *Exception Value: 'CandidateFeedForm' object has no attribute 'ValidationError'* Below is the view that is using the BaseCandFormSet code that I included in the original message. Maybe something is wrong in it? Thanks in advance for your suggestions. I've spent a few days using the documentation and trying to get this to work. I really appreciate the help. ... *def state_office(request): CandFormSet = modelformset_factory(CandidateFeed, **queryset=CandidateFeed.objects.all().order_by('last_name', 'county'),** formset=BaseCandFormSet, extra=0) if request.method == 'POST': formset = CandFormSet(request.POST, request.FILES) if formset.is_valid(): formset.save() return HttpResponseRedirect('/foo/bar/') else: formset = CandFormSet(queryset=CandidateFeed.objects.all().order_by('last_name', 'county')) return render_to_response('state_office.html', { 'formset': formset })* -- 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 validation in model formset
? 11 ?.?. 2553 23:44 Duane Hilton?: > Hi all, > > I'm trying to do some custom validation on a model formset, but I'm > not having good luck. Thank you in advance for your suggestions ... > > In my views, I have: > > class BaseCandFormSet(BaseModelFormSet): >def clean(self): >super(BaseCandFormSet, self).clean() >for form in self.forms: >x = form['x_field'] >y = form['y_field'] >if y < x: >raise forms.ValidationError('Y cannot be less than x.') > > I get the following error: > > Exception Value: 'list' object has no attribute 'ValidationError' On my mobile so can't check but I suspect forms is a list of form objects, try form.validation error instead... Good luck! Jonathan > -- > 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. > -- 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 Validation in Django 1.0.2 Final
Hi, Malcolm... Thanks a lot... It is working Now.. On Apr 7, 12:05 pm, Malcolm Tredinnickwrote: > On Mon, 2009-04-06 at 23:58 -0700, Harish wrote: > > [...] > > > If salary is found negative it displays the error message in beginning > > of the form(that is after the message 'Please correct the error below. > > '). Actually it should display the error message in the 'Salary' > > section > > > I think, I am missing something here. > > Since your field validation appears to only depend on the "salary" form > field, the easiest solution here is to write a clean_salary() method, > rather than using clean(). Then, any ValidationError raised will > automatically be assigned to the salary field. > > Have a look > athttp://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-a...and > the surrounding documentation for some more information about this. In > general, the clean() method is only useful if you're validating multiple > fields simultaneously to check something. When the result of a particular > check only depends on a single field, best to validate as part of that > field's cleaning routine. > > Regards, > Malcolm --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Custom Validation in Django 1.0.2 Final
On Mon, 2009-04-06 at 23:58 -0700, Harish wrote: [...] > If salary is found negative it displays the error message in beginning > of the form(that is after the message 'Please correct the error below. > '). Actually it should display the error message in the 'Salary' > section > > I think, I am missing something here. Since your field validation appears to only depend on the "salary" form field, the easiest solution here is to write a clean_salary() method, rather than using clean(). Then, any ValidationError raised will automatically be assigned to the salary field. Have a look at http://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-a-specific-field-attribute and the surrounding documentation for some more information about this. In general, the clean() method is only useful if you're validating multiple fields simultaneously to check something. When the result of a particular check only depends on a single field, best to validate as part of that field's cleaning routine. Regards, Malcolm --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Custom validation in view?
Thanks Alex that's exactly what I was looking for! Paddy On Apr 5, 10:40 am, Alex Gaynorwrote: > On Sat, Apr 4, 2009 at 8:37 PM, Paddy Joy wrote: > > > I need to validate some data from a form however I need access to the > > request object as the data validation will be different depending on > > the logged on user. > > > What is the preferred method for this type of validation? Can i > > validate thee data in the view and manually set the form field errors? > > > Paddy > > Take a look at number 2 > here:http://collingrady.wordpress.com/2008/07/24/useful-form-tricks-in-dja... > basically you'll just store user on your form object and then you can use it > in your clean_ methods. > > Alex > > -- > "I disapprove of what you say, but I will defend to the death your right to > say it." --Voltaire > "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Custom validation in view?
On Sat, Apr 4, 2009 at 8:37 PM, Paddy Joywrote: > > I need to validate some data from a form however I need access to the > request object as the data validation will be different depending on > the logged on user. > > What is the preferred method for this type of validation? Can i > validate thee data in the view and manually set the form field errors? > > Paddy > > > > Take a look at number 2 here: http://collingrady.wordpress.com/2008/07/24/useful-form-tricks-in-django/ basically you'll just store user on your form object and then you can use it in your clean_ methods. Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: custom validation (clean_) based on image height and width
Perfect. Thank you! On Thu, Mar 19, 2009 at 3:36 PM, Alex Gaynorwrote: > > > On Thu, Mar 19, 2009 at 3:31 PM, Michael Repucci wrote: > >> >> I'm struggling with what seems like should be a very straightforward >> task. I have a model with a field named image of type ImageField, and >> I'd like to do custom validation via the model form's clean_image >> method based on the height and width (in pixels) of the image. >> >> If I just grab the image object from the cleaned_data dictionary it >> doesn't have attributes height and width, and if I specify the >> height_field and width_field parameters in the ImageField >> instantiation, I don't have access to those fields from the >> clean_image method (or do I?). >> >> Is there a simple way to do this that I'm missing? >> >> > Yep, here's an example of a method I use: > > def clean_image(self): > from django.core.files.images import get_image_dimensions > w, h = get_image_dimensions(self.cleaned_data['image']) > if h > 500 or w > 800: > raise forms.ValidationError("The image is too large, the > maximum size is 800x500, your image was %sx%s." % (w, h)) > return self.cleaned_data['image'] > > > -- > "I disapprove of what you say, but I will defend to the death your right to > say it." --Voltaire > "The people's good is the highest law."--Cicero > > > > --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: custom validation (clean_) based on image height and width
On Thu, Mar 19, 2009 at 3:31 PM, Michael Repucciwrote: > > I'm struggling with what seems like should be a very straightforward > task. I have a model with a field named image of type ImageField, and > I'd like to do custom validation via the model form's clean_image > method based on the height and width (in pixels) of the image. > > If I just grab the image object from the cleaned_data dictionary it > doesn't have attributes height and width, and if I specify the > height_field and width_field parameters in the ImageField > instantiation, I don't have access to those fields from the > clean_image method (or do I?). > > Is there a simple way to do this that I'm missing? > > > Yep, here's an example of a method I use: def clean_image(self): from django.core.files.images import get_image_dimensions w, h = get_image_dimensions(self.cleaned_data['image']) if h > 500 or w > 800: raise forms.ValidationError("The image is too large, the maximum size is 800x500, your image was %sx%s." % (w, h)) return self.cleaned_data['image'] -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Custom validation
Well, as i said, overriding save() does not provide the ability to raise an error which is field-specific. What i want to do, was possible with validator_list. I actually don't understand why this functionality has been removed. On Mon, Jul 21, 2008 at 7:59 PM, Jeff FW <[EMAIL PROTECTED]> wrote: > > If that's what you want, then override the save() method of the model > that throws an exception if it doesn't meet your requirements. > > -Jeff > > On Jul 21, 9:43 am, "Alex Rades" <[EMAIL PROTECTED]> wrote: >> The point is that i see this validation as tied to the model, not to a >> specific form. For me this as an integrity check, something which >> should be done at model level. >> >> On Mon, Jul 21, 2008 at 3:20 PM, Jeff FW <[EMAIL PROTECTED]> wrote: >> >> > Sounds like you're looking at the oldforms documentation--that's all >> > been deprecated. Read this instead: >> >http://www.djangoproject.com/documentation/newforms/ >> >> > Especially this part: >> >http://www.djangoproject.com/documentation/newforms/#custom-form-and-... >> >> > -Jeff >> >> > On Jul 21, 8:14 am, "Alex Rades" <[EMAIL PROTECTED]> wrote: >> >> Hi, >> >> >> First of all, I'm using trunk :) >> >> >> I have a couple of models like: >> >> >> class User(model.Model): >> >> group = models.ForeignKey(Group) >> >> >> Class Group(model.Model): >> >> interest = models.ForeignKey(Interests) >> >> >> Basically i want to be possible to change in the admin the interest of >> >> a Group *only if*: >> >> >> self.user_set.count() == 0 >> >> >> The documentation is not very about custom validation, it says to pass >> >> validator_list to the field definition, so i've tried with: >> >> >> interest = models.ForeignKey(interests, validator_list = [ myvalidator ]) >> >> >> But it seems the custom validators are not called at all. Is this >> >> possible? How do I perform custom validation on a specific form? Doing >> >> it into the save() method of the model is not suitable (I want to >> >> raise an error which is specific to a field and is displayed next to >> >> the field itself) >> >> >> Thanks > > > --~--~-~--~~~---~--~~ 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 validation
The point is that i see this validation as tied to the model, not to a specific form. For me this as an integrity check, something which should be done at model level. On Mon, Jul 21, 2008 at 3:20 PM, Jeff FW <[EMAIL PROTECTED]> wrote: > > Sounds like you're looking at the oldforms documentation--that's all > been deprecated. Read this instead: > http://www.djangoproject.com/documentation/newforms/ > > Especially this part: > http://www.djangoproject.com/documentation/newforms/#custom-form-and-field-validation > > -Jeff > > On Jul 21, 8:14 am, "Alex Rades" <[EMAIL PROTECTED]> wrote: >> Hi, >> >> First of all, I'm using trunk :) >> >> I have a couple of models like: >> >> class User(model.Model): >> group = models.ForeignKey(Group) >> >> Class Group(model.Model): >> interest = models.ForeignKey(Interests) >> >> Basically i want to be possible to change in the admin the interest of >> a Group *only if*: >> >> self.user_set.count() == 0 >> >> The documentation is not very about custom validation, it says to pass >> validator_list to the field definition, so i've tried with: >> >> interest = models.ForeignKey(interests, validator_list = [ myvalidator ]) >> >> But it seems the custom validators are not called at all. Is this >> possible? How do I perform custom validation on a specific form? Doing >> it into the save() method of the model is not suitable (I want to >> raise an error which is specific to a field and is displayed next to >> the field itself) >> >> Thanks > > > --~--~-~--~~~---~--~~ 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 validation
Sounds like you're looking at the oldforms documentation--that's all been deprecated. Read this instead: http://www.djangoproject.com/documentation/newforms/ Especially this part: http://www.djangoproject.com/documentation/newforms/#custom-form-and-field-validation -Jeff On Jul 21, 8:14 am, "Alex Rades" <[EMAIL PROTECTED]> wrote: > Hi, > > First of all, I'm using trunk :) > > I have a couple of models like: > > class User(model.Model): > group = models.ForeignKey(Group) > > Class Group(model.Model): > interest = models.ForeignKey(Interests) > > Basically i want to be possible to change in the admin the interest of > a Group *only if*: > > self.user_set.count() == 0 > > The documentation is not very about custom validation, it says to pass > validator_list to the field definition, so i've tried with: > > interest = models.ForeignKey(interests, validator_list = [ myvalidator ]) > > But it seems the custom validators are not called at all. Is this > possible? How do I perform custom validation on a specific form? Doing > it into the save() method of the model is not suitable (I want to > raise an error which is specific to a field and is displayed next to > the field itself) > > Thanks --~--~-~--~~~---~--~~ 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 validation for models in admin
The following did it for me: from django import newforms as forms class yourForm(forms.BaseForm): ... def clean(self): "do your custom validation here" return self.cleaned_data does it for me. Clean is called automatically e.g. by ...is_valid(). hope it helps. jr On Sep 11, 10:19 pm, Mike H <[EMAIL PROTECTED]> wrote: > Hi all, > > I have a model which needs some non-standard checks before it gets saved. > The class looks like this : > > class Page(models.Model): > name = models.CharField(maxlength=30) > content = models.TextField() > start_date = models.DateField() > end_date = models.DateField() > > When I save the model, I want to check that there is no other page with > the same name that has an overlapping start and end date, and cause some > kind of validation error that the admin area will display. > > I have tried the following : > > def save(self): > # check for the overlap happens here > > # if the check fails... > raise validators.ValidationError(_("A page with this name > already exists in the time period specified")) > > However, the admin area does not catch the exception. I've looked > through the djangobook which references a chapter that isn't there yet, > and the online documentation points to a .validate() on the model which > is marked as experimental. If it's possible, where can I put custom > validation like that, that the admin area will pick up on? I'm currently > using 0.96 but could easily upgrade to the svn trunk if needed. > > Thanks for any help, > > Mike --~--~-~--~~~---~--~~ 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 validation for models in admin
Hi Mike, I have the same problem with my model. (Link below) Reading your "points to a .validate() on the model" raises the following question. Have you tried it? http://groups.google.com/group/django-users/browse_thread/thread/bd2b2c24f3879690 --~--~-~--~~~---~--~~ 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 validation for models in admin
Sorry, I should have said that I'd already tried a custom validator. Unfortunately, validators don't provide the functionality I need. I need to check in the database to see if there are any pages, other than the one I am saving, that have start and end dates that overlap with the one I am saving. To do that, I need to know the id of the object I am saving, or if I am trying to save a new object. Validators do not tell me that as the id is not passed in inside all_data. Is there anywhere else I can add this validation? Or is there a type of exception I can raise that the admin area will display as an error and let the user correct the data? Cheers, Mike Collin Grady wrote: > You need to write a validator and use validator_list in the field > attributes to do custom validation in admin. > > http://www.djangoproject.com/documentation/model-api/#validator-list > > > > > --~--~-~--~~~---~--~~ 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 validation for models in admin
You need to write a validator and use validator_list in the field attributes to do custom validation in admin. http://www.djangoproject.com/documentation/model-api/#validator-list --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---