django - Checking if selected many-to-many relation fields are disjoint -


i'm trying create condition has fulfilled before object can saved database. have table , table b, has 2 separate many-to-many relations b. i'm trying check if condition these fields treated sets disjoint holds before saving entry.

here code:

class foo(models.model):   first = models.manytomanyfield(bar, related_name='first')   second = models.manytomanyfield(bar, related_name='second')   def save(self):     if (set(self.first.all()).isdisjoint(list(self.second.all()))):       #save     else:       #raise exception 

but i'm getting error

"< foo: none >" needs have value field "foo" before many-to-many relationship can used.

i'm guessing wants saved before comparison, whole point not save database before condition true. how properly?

model save method have no access m2m related fields because called before them. if want validate them should define custom model form fooadmin class (i suppose you're using django admin) , make validation there.

class fooform(forms.modelform):     class meta:         model = foo         exclude = ()      def clean():         cd = self.cleaned_data         first_objects = cd['first']         second_objects = cd['second']         # logic         return super(fooform, self).clean()   class fooadmin(admin.modeladmin):     form = fooform 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -