def validate_models(): """ Since BaseRunserverCommand is only run once, we need to call model valdidation here to ensure it is run every time the code changes. """ import logging logging.info("Validating models...") error_text = None try: from django.core.management.validation import get_validation_errors try: from cStringIO import StringIO except ImportError: from StringIO import StringIO s = StringIO() num_errors = get_validation_errors(s, None) if num_errors: s.seek(0) error_text = s.read() except ImportError: import django from django.core.checks.model_checks import check_all_models django.setup() errors = check_all_models() if errors: error_text = "\n".join(errors) if error_text: logging.critical("One or more models did not validate:\n%s" % error_text) else: logging.info("All models validated.")
def test_model_check_method_not_shadowed(self): class ModelWithAttributeCalledCheck(models.Model): check = 42 class ModelWithFieldCalledCheck(models.Model): check = models.IntegerField() class ModelWithRelatedManagerCalledCheck(models.Model): pass class ModelWithDescriptorCalledCheck(models.Model): check = models.ForeignKey(ModelWithRelatedManagerCalledCheck) article = models.ForeignKey(ModelWithRelatedManagerCalledCheck, related_name='check') expected = [ Error( "The 'ModelWithAttributeCalledCheck.check()' class method is " "currently overridden by 42.", hint=None, obj=ModelWithAttributeCalledCheck, id='models.E020' ), Error( "The 'ModelWithRelatedManagerCalledCheck.check()' class method is " "currently overridden by %r." % ModelWithRelatedManagerCalledCheck.check, hint=None, obj=ModelWithRelatedManagerCalledCheck, id='models.E020' ), Error( "The 'ModelWithDescriptorCalledCheck.check()' class method is " "currently overridden by %r." % ModelWithDescriptorCalledCheck.check, hint=None, obj=ModelWithDescriptorCalledCheck, id='models.E020' ), ] self.assertEqual(check_all_models(), expected)
def test_model_check_method_not_shadowed(self): class ModelWithAttributeCalledCheck(models.Model): check = 42 class ModelWithFieldCalledCheck(models.Model): check = models.IntegerField() class ModelWithRelatedManagerCalledCheck(models.Model): pass class ModelWithDescriptorCalledCheck(models.Model): check = models.ForeignKey(ModelWithRelatedManagerCalledCheck) article = models.ForeignKey(ModelWithRelatedManagerCalledCheck, related_name='check') expected = [ Error( "The 'ModelWithAttributeCalledCheck.check()' class method is " "currently overridden by 42.", hint=None, obj=ModelWithAttributeCalledCheck, id='models.E020'), Error( "The 'ModelWithRelatedManagerCalledCheck.check()' class method is " "currently overridden by %r." % ModelWithRelatedManagerCalledCheck.check, hint=None, obj=ModelWithRelatedManagerCalledCheck, id='models.E020'), Error( "The 'ModelWithDescriptorCalledCheck.check()' class method is " "currently overridden by %r." % ModelWithDescriptorCalledCheck.check, hint=None, obj=ModelWithDescriptorCalledCheck, id='models.E020'), ] self.assertEqual(check_all_models(), expected)