#24731: ManyToMany model clean validation of related fields
-------------------------------------+-------------------------------------
Reporter: blah73 | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.8
(models, ORM) | Keywords: ManyToMany clean
Severity: Normal | validation
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
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
--
Ticket URL: <https://code.djangoproject.com/ticket/24731>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/049.a2df4c7a8f22f04140b85198633b9d3c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.