Friday, April 8, 2011

Re: Notify user at admin interface if custom save fails



On Friday, April 8, 2011 9:26:05 AM UTC+1, shadyabhi wrote:
I have a class in models.py like:

    class UserProfile(models.Model):
        user = models.ForeignKey(User)
        is_counter = models.BooleanField()
        is_student = models.BooleanField()
       
        def save(self):
            if self.is_counter != self.is_student:
                super(UserProfile, self).save()
            else:
                return False

I have defined two types of user, either it can be a student or counterbut never both.
I do not want the user to be both coutner as well as student. So, I defined a custom save() such that entry is added to database when is_counter != is_student.

Now, when I use Admin interface to add inconsistent entry, the interface says entry added successfully but "actually" it isn't added. How can I add an error message that will be displayed when *False* is returned by save().
 
This is the wrong place to be doing this check. If you want to validate something, you should do it in the validation step. See http://docs.djangoproject.com/en/1.3/ref/models/instances/#validating-objects - you probably want to override Model.clean().
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment