Validation on ManyToManyField before Save in Models.py

Posted by Heyl1 on Stack Overflow See other posts from Stack Overflow or by Heyl1
Published on 2010-06-16T10:26:20Z Indexed on 2010/06/17 10:43 UTC
Read the original article Hit count: 255

Filed under:
|
|
|

I have the following models:

class Application(models.Model):
 users = models.ManyToManyField(User, through='Permission')
 folder = models.ForeignKey(Folder)

class Folder(models.Model):
 company = models.ManyToManyField(Compnay)

class UserProfile(models.Model):
 user = models.OneToOneField(User, related_name='profile')
 company = models.ManyToManyField(Company)

What I would like to do is to check whether one of the users of the Application has the same company as the Application (via Folder). If this is the case the Application instance should not be saved.

The problem is that the ManyToManyFields aren't updated until after the 'post-save' signal.
The only option seems to be the new m2m_changed signal. But I'm not sure how I then roll back the save that has already happened.
Another option would be to rewrite the save function (in models.py, because I'm talking about the admin here), but I'm not sure how I could access the manytomanyfield content.
Finally I've read something about rewriting the save function in the admin of the model in admin.py, however I still wouldn't know how you would access the manytomanyfield content.

I have been searching for this everywhere but nothing I come across seems to work for me.
If anything is unclear, please tell me.

Thanks for your help!
Heleen

© Stack Overflow or respective owner

Related posts about python

Related posts about django