Opened 13 years ago
Closed 13 years ago
#17394 closed Uncategorized (invalid)
Models in a package breaks model validation
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
If I create an app:
$ ./manage.py startapp test_app
I then create a package for my models:
$ cd test_app $ mkdir models $ cd models $ touch __init__.py
I then create an invalid model in test_app/models/bar.py:
from django.db import models class Foo(models.Model): class Meta: app_label = 'test_app.models' invalid_char_field = models.CharField() # should have max_length
Finally, I import this in my tests, I chose test_app/tests.py:
from django.test import TestCase from models.bar import Bar class SimpleTest(TestCase): def test_basic_addition(self): """ Tests that 1 + 1 always equals 2. """ self.assertEqual(1 + 1, 2)
This has a really weird effect on model validation. Calling validate works:
$ ./manage.py validate 0 errors found
But tests seem to have a different validation path, and fail as expected:
$ ./manage.py test test_app Creating test database for alias 'default'... Error: One or more models did not validate: test_app.foo: "invalid_char_field": CharFields require a "max_length" attribute that is a positive integer.
I know this isn't a common use case, but I have an old project that has models like this. #2289 suggests I should be able to do this. I can work around it by adding an import for every single model in test_app/models/*.py
inside of test_app/__init__.py
but that introduces name clashes.
Thanks.
Change History (6)
comment:1 by , 13 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
comment:2 by , 13 years ago
Resolution: | needsinfo |
---|---|
Status: | closed → reopened |
Ah, sorry. Behaviour is exactly the same with the correct value for app_label.
comment:4 by , 13 years ago
I did, but the behaviour is the same both with and without test_app/models.py.
comment:5 by , 13 years ago
What happens if you put from bar import *
in your models/__init__.py
file?
comment:6 by , 13 years ago
Resolution: | → invalid |
---|---|
Status: | reopened → closed |
As pointed out in charettes' latest comment, the problem is that your model isn't detected by validate, because it isn't imported in the test_app.models
package.
What happens when you use a correct value for app_label?: