Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#10337 closed (invalid)

syncdb fails to load initial_data with certain __init__.py

Reported by: Torsten Bronger Owned by: nobody
Component: Core (Management commands) Version: dev
Severity: Keywords:
Cc: bronger@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If the application's __init__.py contains

from django.contrib.auth import models

"manage.py syncdb" doesn't load the init_data fixture for this application.

import django.contrib.auth.models

doesn't cause this problem.

Change History (5)

comment:1 by Russell Keith-Magee, 15 years ago

Resolution: worksforme
Status: newclosed

I can't replicate this problem. I can't even think of a way that I _could_ replicate this problem - there isn't anything that should link the import method to initial_data fixtures.

If you can provide a complete set of instructions (or preferably, a full project definition) that demonstrates the problem, please reopen.

comment:2 by Alex Gaynor, 15 years ago

I can see why that would break something, but it's not something wrong with django, in python's import process when it sees from module import something the first place it checks is if that item exists in module/init.py, this isn't something Django could fix or work around or anything like that, it's just how python works.

comment:3 by Torsten Bronger, 15 years ago

Resolution: worksforme
Status: closedreopened

I can reproduce it with the following steps:

  1. django-admin.py startproject myproject
  2. ./manage.py startapp myapp
  3. Add a fixture called initial_data which e.g. simply adds a user in myapp/fixtures/
  4. set the database settings in settings.py and add "myproject.myapp" to INSTALLED_APPS
  5. create the database in the database backend
  6. call syncdb. The fixture is loaded.
  7. Add myapp/init.py with the above mentioned line
  8. drop&create the database in the database backend
  9. call syncdb. The fixture is not loaded.

As to what Alex said, sorry but I don't understand it. Be that as it may, I don't see why a high-level functionality like fixture loading depends on how a module is loaded. Currently it does, but it needn't.

comment:4 by Alex Gaynor, 15 years ago

Resolution: invalid
Status: reopenedclosed

When python tries to import the models from your application so that it can instantiate them for the initial_data it's import includes the models path(obviously) however python import semantics say to look in the init of the app for the models name, therefore it can't ever find your applications models since it gets the models name from init which doesn't contain your models.

http://docs.python.org/reference/simple_stmts.html#the-import-statement
http://docs.python.org/library/functions.html#__import

comment:5 by Torsten Bronger, 15 years ago

Granted, but my application doesn't have models of its own anyway. myapp/models.py is deliberately empty. Instead, the fixture adds "User" instances from the auth module.

In other words, why does Django assume that a fixture only adds instances of the app's models? For example, User instances can be a very sensible thing to add.

Note: See TracTickets for help on using tickets.
Back to Top