Minor concern with the "apps" convention

142 views
Skip to first unread message

Aymeric Augustin

unread,
Jan 26, 2014, 7:54:09 AM1/26/14
to django-d...@googlegroups.com
Hello,

Django ≥ 1.7 recommends to define AppConfig subclasses for a given application in apps.py.

If the application also imports the app registry in its __init__.py with `from django.apps import apps`, this definition of `apps` will conflict with the `apps` submodule.

I’ve worked around this problem in django.contrib.auth and django.contrib.comments (which is deprecated) with: `from django.apps import apps as django_apps`.

If anything, this is one more reason why it’s a bad idea to put code in __init__.py. Given that we have a simple workaround, I’m inclined to simply ignore this pitfall.

If someone has a better idea, let me know!

--
Aymeric.



Russell Keith-Magee

unread,
Jan 26, 2014, 7:40:45 PM1/26/14
to Django Developers
I'm inclined to agree. Python imports can be complex when name clashes occur; the best course of action is to just avoid the problem in the first place. 

It's worth documenting this as a gotcha in the app loading docs (with the admonition of "this is why you don't put code in __init__.py"), but I don't think we need to be overly concerned.

Yours,
Russ Magee %-)

Aymeric Augustin

unread,
Feb 2, 2014, 9:32:45 AM2/2/14
to django-d...@googlegroups.com
On 27 janv. 2014, at 01:40, Russell Keith-Magee <rus...@keith-magee.com> wrote:

> It's worth documenting this as a gotcha in the app loading docs (with the admonition of "this is why you don't put code in __init__.py"), but I don't think we need to be overly concerned.

Done: https://github.com/django/django/commit/ab2f21080d8b3112c1ba9a0bf923eae733be4242

--
Aymeric.

Reply all
Reply to author
Forward
0 new messages