Monday, January 16, 2017

Re: Am I stupid or is there an essential error in Django 1.10 Docs?

On Mon, Jan 16, 2017 at 12:23:09PM -0800, 'Peter Müller' via Django users wrote:
> Am Montag, 16. Januar 2017 21:20:53 UTC+1 schrieb Daniel Roseman:
> > The traceback shows that you are importing your views module in the
> > __init__.py of your app. Don't do that.
>
> Well else python doesn't find the view.py file?

Python will be able to import db_testing.views just fine even if you
don't import viewsi from . in db_testing/__init__.py. All Python cares
about is that there is a correct Python file in the correct location.

If you're interested in more details about what's going wrong here,
there's an explanation here:
https://docs.djangoproject.com/en/1.10/ref/applications/#how-applications-are-loaded

In particular, the following paragraphs:

> At this stage, your code shouldn't import any models!
>
> In other words, your applications' root packages and the modules
> that define your application configuration classes shouldn't import
> any models, even indirectly.

What's happening here is that the root of your "db_testing" package
(i.e. db_testing/__init__.py) indirectly imports models, because it
imports the views module, which in turn imports models.

Just removing the "from . import views" line should let you move
forward – unless you also have other imports in the __init__.py file,
in which case you can most likely just remove them, too. In general,
in an average Django package, there's little reason to import stuff in
its root package.

Good luck,

Michal

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20170117070025.GN1628%40koniiiik.org.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment