Re: Mutually exclusive fields in model validation
On Sep 15, 11:19 pm, Sonal Breedwrote: > Thanks a lot Cliff, I implemented it in a similar manner, just wanted > to know if we > have anything like validation rules a la Access. > > Thanks again, > Sincerely, > Sonal. Not yet. Honza Kral's Google Summer of Code project for model validation is due to be merged reasonably soon though, so this will be in version 1.2 - or in trunk at some earlier point if you don't want to wait that long. -- DR. --~--~-~--~~~---~--~~ 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: Mutually exclusive fields in model validation
Thanks a lot Cliff, I implemented it in a similar manner, just wanted to know if we have anything like validation rules a la Access. Thanks again, Sincerely, Sonal. On Sep 15, 3:08 pm, "J. Cliff Dyer"wrote: > Override the save method on your model, something like: > > class MyModel(models.Model): > field1 = models.TextField(blank=True) > field2 = models.TextField(blank=True) > > def save(self): > if (self.field1 and self.field2): > raise ModelValidationError, "only one can live" > elif (not self.field1 and not self.field2): > raise ModelValidationError, "one must live" > else: > super(MyModel, self).save() > > Untested code. I don't remember if ModelValidationError exists, and I > don't know if you need to tweak the signature on your save method, but > that should get you pointed in the right direction. > > Cheers, > Cliff > > On Tue, 2009-09-15 at 14:33 -0700, Sonal Breed wrote: > > Hi all, > > > I have a model wherein I want to put a validation as > > You must enter Field1 or Field2, but not both. > > > This model is at the back end and not visible through forms. > > How do I accomplish it? > > > Thanks, > > > Sincerely, > > Sonal --~--~-~--~~~---~--~~ 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: Mutually exclusive fields in model validation
Override the save method on your model, something like: class MyModel(models.Model): field1 = models.TextField(blank=True) field2 = models.TextField(blank=True) def save(self): if (self.field1 and self.field2): raise ModelValidationError, "only one can live" elif (not self.field1 and not self.field2): raise ModelValidationError, "one must live" else: super(MyModel, self).save() Untested code. I don't remember if ModelValidationError exists, and I don't know if you need to tweak the signature on your save method, but that should get you pointed in the right direction. Cheers, Cliff On Tue, 2009-09-15 at 14:33 -0700, Sonal Breed wrote: > Hi all, > > I have a model wherein I want to put a validation as > You must enter Field1 or Field2, but not both. > > This model is at the back end and not visible through forms. > How do I accomplish it? > > Thanks, > > Sincerely, > Sonal > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Mutually exclusive fields in model validation
Hi all, I have a model wherein I want to put a validation as You must enter Field1 or Field2, but not both. This model is at the back end and not visible through forms. How do I accomplish it? Thanks, Sincerely, Sonal --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---