Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 917023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:08:36+00:00 2026-05-15T18:08:36+00:00

I have the following models: class Application(models.Model): users = models.ManyToManyField(User, through=’Permission’) folder = models.ForeignKey(Folder)

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:08:37+00:00Added an answer on May 15, 2026 at 6:08 pm

    Because I didn’t get a reply from Botondus I decided to ask a new question in the Django Users Google Group and finally got the answer from jaymz.

    I figured that Botondus method was the right way of doing it, it just wasn’t quite working. The reason that it doesn’t work in this case is because I’m using a Through model for the field I would like to do the validation on. Because of some earlier feedback I got on a previously posted question I gathered that first the Application instance is saved and then the ManyToMany instances are saved (I believe this is right, but correct me if I’m wrong). So I thought that, if I would perform the validation on the ManyToMany Field in the Through model, this would not prevent the Application instance being saved. But in fact it does prevent that from happening.

    So if you have a ManyToMany Field inline in your model’s admin and you would like to do validation on that field, you specify the clean function in the through model, like this:

    admin.py
    class PermissionInline(admin.TabularInline):
        form = PermissionForm
        model = Permission
        extra = 3
    
    forms.py
    class PermissionForm(forms.ModelForm):
        class Meta:
            model = Permission
    
        def clean(self):
            cleaned_data = self.cleaned_data
            user = cleaned_data['user']
            role = cleaned_data['role']
            if role.id != 1:
                folder = cleaned_data['application'].folder
                if len(filter(lambda x:x in user.profile.company.all(),folder.company.all())) > 0: # this is an intersection
                    raise forms.ValidationError("One of the users of this Application works for one of the Repository's organisations!")
            return cleaned_data 
    

    If the validation results in an error NOTHING (neither the application instance, nor the manytomany users instances) is saved and you get the chance to correct the error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
I have following models setup in my Django application class School(models.Model): name = models.TextField()
I have the following models: class ProjectUser(models.Model): categories = models.ManyToManyField('UserCategory', blank=True, null=True) user_id =
Let's assume that I have following models: class ScoutBook(models.Model): troop = models.ForeignKey('Dictionary', limit_choices_to={'type' :
I have 2 models class Vhost(models.Model): dns = models.ForeignKey(DNS) user = models.ForeignKey(User) extra =
In a django application I have the following model: class Appointment(models.Model): #some other fields
I have the following models: class UserProfile(models.Model): user = models.OneToOneField(User) score = models.PositiveIntegerField() class
In an ASP.NET MVC application where I have the following model.. public class EventViewModel
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.