[Django] #24731: ManyToMany model clean validation of related fields

28 views
Skip to first unread message

Django

unread,
Apr 30, 2015, 11:55:12 AM4/30/15
to django-...@googlegroups.com
#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.

Django

unread,
Apr 30, 2015, 12:50:17 PM4/30/15
to django-...@googlegroups.com
#24731: ManyToMany model clean validation of related fields
-------------------------------------+-------------------------------------
Reporter: blah73 | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: ManyToMany clean | Triage Stage:
validation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* needs_better_patch: => 0
* resolution: => needsinfo
* needs_tests: => 0
* needs_docs: => 0


Comment:

I'm not aware of any work or proposals that would make something like this
possible. Since we'd need to make some decisions on an API, discussion
should first happen on the DevelopersMailingList. I'll close this ticket
until we have an action plan.

Possible solution using signals: http://schinckel.net/2012/02/06/pre-
validating-many-to-many-fields./

--
Ticket URL: <https://code.djangoproject.com/ticket/24731#comment:1>

Django

unread,
Apr 30, 2015, 2:34:34 PM4/30/15
to django-...@googlegroups.com
#24731: ManyToMany model clean validation of related fields
-------------------------------------+-------------------------------------
Reporter: blah73 | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: ManyToMany clean | Triage Stage:
validation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by blah73):

Thanks for the link. Using m2m signals looks like it will work and I plan
on implementing it. However it does add additional complexity compared to
normal model validation. I'll make a post on the developers mailing list.

--
Ticket URL: <https://code.djangoproject.com/ticket/24731#comment:2>

Django

unread,
Dec 3, 2015, 11:10:12 AM12/3/15
to django-...@googlegroups.com
#24731: ManyToMany model clean validation of related fields
-------------------------------------+-------------------------------------
Reporter: blah73 | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: ManyToMany clean | Triage Stage:
validation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by nemesisdesign):

Hi everybody,

I also think this is an area for improvement in django.

I've posted on django-developers: https://groups.google.com/forum/#!topic
/django-developers/pQ-8LmFhXFg

I quote the relevant bits:

----

I spent a few hours working on this issue, I consider myself fluent with
django and it's quite shocking I had to put such an amount of effort just
to validate many2many relationships before they are saved to the database.

IMHO it would be better if we could do one of these two (or even both)
things:

1. make this process easier in future django versions
2. document the current best practice to solve this problem in current
django to save people's time

What do people you think?

Here's my solution working with django 1.9:
https://github.com/openwisp/django-netjsonconfig/compare/4082988...master

What do you think of it? Can it be improved in some way?

----

Keep up the good work
Federico Capoano

--
Ticket URL: <https://code.djangoproject.com/ticket/24731#comment:3>

Django

unread,
Mar 21, 2018, 2:34:32 PM3/21/18
to django-...@googlegroups.com
#24731: ManyToMany model clean validation of related fields
-------------------------------------+-------------------------------------
Reporter: blah73 | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: ManyToMany clean | Triage Stage:
validation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Geoffrey Fairchild):

This ticket was closed about 3 years ago. I just encountered this issue,
and it's quite irritating. It definitely seems like something should be
done about this.

--
Ticket URL: <https://code.djangoproject.com/ticket/24731#comment:4>

Django

unread,
Mar 21, 2018, 3:26:30 PM3/21/18
to django-...@googlegroups.com
#24731: ManyToMany model clean validation of related fields
-------------------------------------+-------------------------------------
Reporter: blah73 | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: ManyToMany clean | Triage Stage:
validation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

As I said in comment one, if there is some consensus about how to proceed,
the ticket may be reopened.

--
Ticket URL: <https://code.djangoproject.com/ticket/24731#comment:5>

Django

unread,
Jul 7, 2022, 8:23:19 AM7/7/22
to django-...@googlegroups.com
#24731: ManyToMany model clean validation of related fields
-------------------------------------+-------------------------------------
Reporter: blah73 | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: ManyToMany clean | Triage Stage:
validation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by ldeluigi):

I'd put a catch for ValidationError or IntegrityError inside the
save_form_data method that would catch signals that throw them, for
example the m2m_changed 'post_add'

--
Ticket URL: <https://code.djangoproject.com/ticket/24731#comment:6>

Reply all
Reply to author
Forward
0 new messages