Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7908: Added validation checks on attempts to create ForeignKey…

… and M2M relations with abstract classes. Thanks to Rock Howard for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8442 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f748fa27629b8824d30866e4687bb91c264dfb65 1 parent c0727ad
Russell Keith-Magee authored August 19, 2008
4  django/core/management/validation.py
@@ -68,7 +68,7 @@ def get_validation_errors(outfile, app=None):
68 68
             # fields, m2m fields, m2m related objects or related objects
69 69
             if f.rel:
70 70
                 if f.rel.to not in models.get_models():
71  
-                    e.add(opts, "'%s' has relation with model %s, which has not been installed" % (f.name, f.rel.to))
  71
+                    e.add(opts, "'%s' has a relation with model %s, which has either not been installed or is abstract." % (f.name, f.rel.to))
72 72
                 # it is a string and we could not find the model it refers to
73 73
                 # so skip the next section
74 74
                 if isinstance(f.rel.to, (str, unicode)):
@@ -105,7 +105,7 @@ def get_validation_errors(outfile, app=None):
105 105
             # existing fields, m2m fields, m2m related objects or related
106 106
             # objects
107 107
             if f.rel.to not in models.get_models():
108  
-                e.add(opts, "'%s' has m2m relation with model %s, which has not been installed" % (f.name, f.rel.to))
  108
+                e.add(opts, "'%s' has an m2m relation with model %s, which has either not been installed or is abstract." % (f.name, f.rel.to))
109 109
                 # it is a string and we could not find the model it refers to
110 110
                 # so skip the next section
111 111
                 if isinstance(f.rel.to, (str, unicode)):
6  django/db/models/fields/related.py
@@ -646,6 +646,7 @@ def __init__(self, to, to_field=None, rel_class=ManyToOneRel, **kwargs):
@@ -756,6 +757,11 @@ def contribute_to_related_class(self, cls, related):
17  tests/modeltests/invalid_models/models.py
@@ -5,7 +5,7 @@
5 5
 """
6 6
 
7 7
 from django.db import models
8  
-model_errors = ""
  8
+
9 9
 class FieldErrors(models.Model):
10 10
     charfield = models.CharField()
11 11
     decimalfield = models.DecimalField()
@@ -168,6 +168,15 @@ class RelationshipDoubleFK(models.Model):
168 168
     third = models.ForeignKey(Group, related_name="rel_to_set")
169 169
     date_added = models.DateTimeField()
170 170
 
  171
+class AbstractModel(models.Model):
  172
+    name = models.CharField(max_length=10)
  173
+    class Meta:
  174
+        abstract = True
  175
+
  176
+class AbstractRelationModel(models.Model):
  177
+    fk1 = models.ForeignKey('AbstractModel')
  178
+    fk2 = models.ManyToManyField('AbstractModel')
  179
+    
171 180
 model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute.
172 181
 invalid_models.fielderrors: "decimalfield": DecimalFields require a "decimal_places" attribute.
173 182
 invalid_models.fielderrors: "decimalfield": DecimalFields require a "max_digits" attribute.
@@ -250,8 +259,8 @@ class RelationshipDoubleFK(models.Model):
250 259
 invalid_models.selfclashm2m: Accessor for m2m field 'm2m_4' clashes with related m2m field 'SelfClashM2M.selfclashm2m_set'. Add a related_name argument to the definition for 'm2m_4'.
251 260
 invalid_models.selfclashm2m: Reverse query name for m2m field 'm2m_3' clashes with field 'SelfClashM2M.selfclashm2m'. Add a related_name argument to the definition for 'm2m_3'.
252 261
 invalid_models.selfclashm2m: Reverse query name for m2m field 'm2m_4' clashes with field 'SelfClashM2M.selfclashm2m'. Add a related_name argument to the definition for 'm2m_4'.
253  
-invalid_models.missingrelations: 'rel2' has m2m relation with model Rel2, which has not been installed
254  
-invalid_models.missingrelations: 'rel1' has relation with model Rel1, which has not been installed
  262
+invalid_models.missingrelations: 'rel1' has a relation with model Rel1, which has either not been installed or is abstract.
  263
+invalid_models.missingrelations: 'rel2' has an m2m relation with model Rel2, which has either not been installed or is abstract.
255 264
 invalid_models.grouptwo: 'primary' has a manually-defined m2m relation through model Membership, which does not have foreign keys to Person and GroupTwo
256 265
 invalid_models.grouptwo: 'secondary' has a manually-defined m2m relation through model MembershipMissingFK, which does not have foreign keys to Group and GroupTwo
257 266
 invalid_models.missingmanualm2mmodel: 'missing_m2m' specifies an m2m relation through model MissingM2MModel, which has not been installed
@@ -260,4 +269,6 @@ class RelationshipDoubleFK(models.Model):
260 269
 invalid_models.personselfrefm2m: Many-to-many fields with intermediate tables cannot be symmetrical.
261 270
 invalid_models.personselfrefm2m: Intermediary model RelationshipTripleFK has more than two foreign keys to PersonSelfRefM2M, which is ambiguous and is not permitted.
262 271
 invalid_models.personselfrefm2mexplicit: Many-to-many fields with intermediate tables cannot be symmetrical.
  272
+invalid_models.abstractrelationmodel: 'fk1' has a relation with model AbstractModel, which has either not been installed or is abstract.
  273
+invalid_models.abstractrelationmodel: 'fk2' has an m2m relation with model AbstractModel, which has either not been installed or is abstract.
263 274
 """

0 notes on commit f748fa2

Please sign in to comment.
Something went wrong with that request. Please try again.