Opened 10 years ago
Closed 10 years ago
#23349 closed Cleanup/optimization (fixed)
Document that migrations must have dependencies in order to access other apps' models
Reported by: | no | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 1.7-rc-3 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When using migrations.RunPython
the first parameter passed to the function is documented as being equivalent to South's orm['app.Model']
. In South, the orm has a complete view of all the models, whereas the app
parameter only contains apps/models for which the current migration depends on.
I've attached a simple project. To demonstrate this issue, run python manage.py migrate
in the app.
The project contains 3 apps: spam, eggs, and cheese. There are dependencies between spam and eggs, but cheese is isolated. I created an empty migration in eggs, and print all the models I can find via the apps
parameter. I was expecting it to output all of the models in all three apps, but instead it only prints the models for which the migration depends on.
The apps models are as follows:
eggs: Egg (links to spam.Beef)
cheese: Cheese (no links)
spam: Beef (no links), Potato( links to spam.Beef)
I created an empty migration in eggs which lists all the models in the apps parameter, this is what it prints:
# print(cls) for cls in (for cls in apps.get_app_configs()) AppConfigStub: eggs class 'Egg' AppConfigSub: spam class 'Beef' class 'Potato' ---- # print(cls) for cls in apps.get_models() class 'Egg' class 'Beef' class 'Potato'
Notice how it neglects to print out anything from the 'cheese' app.
Attachments (1)
Change History (7)
by , 10 years ago
Attachment: | djtest.zip added |
---|
comment:1 by , 10 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Type: | New feature → Bug |
comment:2 by , 10 years ago
Resolution: | invalid |
---|---|
Status: | closed → new |
I see two options here.
- First,
RunPython
could be documented better to explicitly say that theapp
parameter not exactly like south'sorm
parameter, and instead only includes models for which the migration depends on. - Second, would be to augment the
app
parameter so that if it can't find the requested model, it'll then try to find it fromdjango.apps.apps
, I'm not sure what the ramifications of this are though, but it's how I would expect it to work given the current documentation.
I'd be happy to send a PR on either of these, but I'd likely needs some guidance on the second one.
comment:3 by , 10 years ago
Component: | Migrations → Documentation |
---|---|
Summary: | Migration's project state does not include all models/apps → Document that migrations must have dependencies in order to access other apps' models |
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
I think improving the documentation is the way to go.
comment:4 by , 10 years ago
First draft of better documentation: https://github.com/django/django/pull/3127
I think the language could be simplified a lot, any suggestions?
comment:5 by , 10 years ago
New pull request which fixes the commit message from the previous one. The documentation is also simplified as suggested from the other PR.
comment:6 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Thank you for the report, but I can't see a new feature request in here, so changing this to "Bug".
I can confirm that behavior but I don't think this is really a bug. If you need the
Cheese
model in your migration, add the appropriate migration as a dependency. Closing it as invalid.Adding
('cheese', '0001_initial'),
in('eggs', '0002_auto_20140822_2124')
shows the expected behavior:Please re-open the ticket with a descriptive explanation you'd like to see implemented as a new feature.