Code

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#1686 closed defect (duplicate)

Relative model import should work within app directory

Reported by: ubernostrum Owned by: adrian
Component: Core (Other) Version: magic-removal
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Assume a project with an application called 'weblog', whose models live in the directory weblog/models.py, and with a model class called 'Entry'.

From within the top-level directory for the project, doing 'from weblog.models import Entry' works as expected.

From within the 'weblog' directory, doing 'from models import Entry' should work, but instead produces this traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "models.py", line 17, in ?
    class Category(models.Model):
  File "/Users/jbennett/dev/092/magic-removal/django/db/models/base.py", line 45, in __new__
    new_class._meta.app_label = model_module.__name__.split('.')[-2]
IndexError: list index out of range

It'd be nice if Django could handle this situation, as it makes app-specific URL configuration ever so much nicer (e.g., model imports to get QuerySets for generic views won't need the full path to the model, and hence won't be coupled to the project).

Attachments (1)

base.diff (766 bytes) - added by erob@… 8 years ago.
partial work-around: works with unittest and django.db.models.Model

Download all attachments as: .zip

Change History (3)

comment:1 Changed 8 years ago by ubernostrum

  • Component changed from Admin interface to Core framework

comment:2 Changed 8 years ago by adrian

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #1658.

Changed 8 years ago by erob@…

partial work-around: works with unittest and django.db.models.Model

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.