Skip to content

Commit 9e2eccc

Browse files
Improved model validator to throw error if a model has two ManyToMany relationships to the same model and doesn't set 'singular'. Refs #452.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1513 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent e0f0532 commit 9e2eccc

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

django/core/management.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,13 @@ def get_validation_errors(outfile):
646646
if not type(c) in (tuple, list) or len(c) != 2:
647647
e.add(opts, '"%s" field: "choices" should be a sequence of two-tuples.' % f.name)
648648

649+
# Check for multiple ManyToManyFields to the same object, and
650+
# verify "singular" is set in that case.
651+
for i, f in enumerate(opts.many_to_many):
652+
for previous_f in opts.many_to_many[:i]:
653+
if f.rel.to == previous_f.rel.to and f.rel.singular == previous_f.rel.singular:
654+
e.add(opts, 'The "%s" field requires a "singular" parameter, because the %s model has more than one ManyToManyField to the same model (%s).' % (f.name, opts.object_name, previous_f.rel.to.object_name))
655+
649656
# Check admin attribute.
650657
if opts.admin is not None:
651658
if not isinstance(opts.admin, meta.Admin):

0 commit comments

Comments
 (0)