Code

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#10337 closed (invalid)

syncdb fails to load initial_data with certain __init__.py

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

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.

Attachments (0)

Change History (5)

comment:1 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

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 Changed 5 years ago by Alex

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 Changed 5 years ago by bronger

  • Resolution worksforme deleted
  • Status changed from closed to reopened

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 Changed 5 years ago by Alex

  • Resolution set to invalid
  • Status changed from reopened to closed

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 Changed 5 years ago by bronger

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.