﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24731	ManyToMany model clean validation of related fields	blah73	nobody	"Suppose I have a model with a ManyToManyField similar to this one (the model Word has a language, too):

{{{
class Sentence(models.Model):
    words = models.ManyToManyField(Word)
    language = models.ForeignKey(Language)
    def clean(self):
        for word in self.words.all():
            if word.language_id != self.language_id:
                raise ValidationError('One of the words has a false language')

}}}

When trying to add a new sentence (e.g. through django admin) I get 'Sentence' needs to have a value for field ""sentence"" before this many-to-many relationship can be used.

Apparently the only way to fix this is to put the validation logic in a forms.ModelForm and then add that form to the admin.ModelAdmin.  This fixes the issue with the admin input but now my API is fragile.  (Directly API access can add invalid data)

Is this part of a larger issue with Many to Many fields?  Is there anything in progress that would alleviate this specific issue?  I understand that the ManyToMany is pretty complex and this would likely require restructuring it.  I'd like to help out if I possible... the current work around solution is kinda dangerous from a Django user perspective. 



I borrowed this example from the following source:
http://stackoverflow.com/questions/7986510/django-manytomany-model-validation

and made a minimal example case in Django 1.8:
https://github.com/blah73/mmtest




"	New feature	closed	Database layer (models, ORM)	1.8	Normal	needsinfo	ManyToMany clean validation		Unreviewed	0	0	0	0	0	0
