Opened 12 years ago

Closed 12 years ago

#17765 closed Bug (worksforme)

Better model 'app_label' introspection using 'INSTALLED_APPS'

Reported by: fengb Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords: app_label
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently, the model generates "app_label" by taking the second to last module:

django.contrib.auth.models.User -> auth
newproj.newapp.models.Newmodel -> newapp
newproj.newapp.models.db.Newmodel -> models

The last example doesn't make any sense. I propose that we match the module chain against 'INSTALLED_APPS' to find the correct app.

Change History (4)

comment:1 by anonymous, 12 years ago

Needs tests: set

I have an implementation available on Github:

https://github.com/fengb/django/tree/app-label-introspect

Tests are failing but they look somewhat unrelated. I do not have enough context to fix them.

comment:2 by fengb, 12 years ago

Crap, I didn't notice that my description formatting gotten eaten...

Middle "paragraph" should be:

  • django.contrib.auth.models.User -> auth
  • newproj.newapp.models.Newmodel -> newapp
  • newproj.newapp.models.db.Newmodel -> models

comment:3 by Aymeric Augustin, 12 years ago

Has patch: set
Triage Stage: UnreviewedAccepted
Type: New featureBug

Your proposal looks correct. But maybe Django should just forbid to define modules outside of <app_label>.models — this deserves a bit more investigation.

Maybe we should raise an error when app_label isn't in INSTALLED_APPS. Currently it's possible to import models from an app that isn't in INSTALLED_APPS, and it can lead to hard-to-debug errors. This is related to the app-refactor project — see #3591.

Last edited 12 years ago by Aymeric Augustin (previous) (diff)

comment:4 by Aymeric Augustin, 12 years ago

Resolution: worksforme
Status: newclosed

I just remembered that Django provide an option exactly for this:

https://docs.djangoproject.com/en/dev/ref/models/options/#app-label

Last edited 12 years ago by Aymeric Augustin (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top