I have the following models (simplified example):

class Book(models.Model):
 users = models.ManyToManyField(User, through=Permission)

class Permission(models.Model):
 user = models.ForeignKey(User)
 role = models.ForeignKey(Group)
 active = models.BooleanField()
 book = models.ForeignKey(Book)

What I need is that for a Book instance there cannot be more than one
User of with the same Role and Active. So this is allowed:

Alice, Admin, False (not active), BookA
Dick, Admin, True (active), BookA
Chris, Editor, False (not active), BookA
Matt, Editor, False (not active), BookA

But this is not allowed:

Alice, Admin, True (active), BookA
Dick, Admin, True (active), BookA

Now this cannot be done using unique_together, because it only counts
when active is True. I've tried to write a custom clean/
validate_unique method (like how I have done here:
http://stackoverflow.com/questions/3052427/validation-on-manytomanyfield-before-save-in-models-py/3266169#3266169).
But it seems that when you save a Book and it runs the validation on
each Permission, the already validated Permission instances aren't
saved until they've all been validated. This makes sense, because you
don't want them to be saved in case something doesn't validate.

Could anyone tell me if there is a way to perform the validation
described above?

P.S. I could imagine using the savepoint feature (http://
docs.djangoproject.com/en/1.2/topics/db/transactions/), but I only
really want to consider that as a last resort.

Note! I first posted this question on StackOverflow. I received a
response but the answer wouldn't work for me. Please have a look at
the response to avoid repeating the answer: 
http://stackoverflow.com/questions/3759687/

-- 
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.

Reply via email to