How to make an app independent on a specific site?

11 views
Skip to first unread message

Jim

unread,
Aug 24, 2011, 12:09:57 AM8/24/11
to django...@googlegroups.com
Greetings, everyone.

I am new to Django, and much of my knowledge comes from the Django book version 2. In the book, a site, mysite, is created for demo purposes. And an app, books, is also created for demo purposes. In the file, mysite/books/admin.py, there is a line like this:
from mysite.books.models import Publisher, Author, Book
It seems to me that this line makes the app dependent on the name of the site, which is mysite. There is a possibility that an app you have developed for a site might turn out to have generic use. So, I think it is a good idea to always keep an app independent on the site as much as possible. 

Here is the question. Is it possible that to store the site name into a variable, such as site_name, then construct a statement like the following?

statement = 'from ' + site_name + '.books.models import Publisher' exec(statement) # I am not sure if exec is a legitimate function. Here is just an example








Jim

unread,
Aug 24, 2011, 12:13:01 AM8/24/11
to django...@googlegroups.com
BTW, the way I posted above isn't so elegant. Is there a better way to do this? Any thoughts?

Mike Dewhirst

unread,
Aug 24, 2011, 12:27:26 AM8/24/11
to django...@googlegroups.com
On 24/08/2011 2:09pm, Jim wrote:
> Greetings, everyone.
>
> I am new to Django, and much of my knowledge comes from the Django
> book version 2 <http://www.djangobook.com/en/2.0/>. In the book, a
> site, mysite, is created for demo purposes. And an app, books, is also
> created for demo purposes. In the file, mysite/books/admin.py, there
> is a line like this:
> from mysite.books.models import Publisher, Author, Book
> It seems to me that this line makes the app dependent on the name of
> the site, which is mysite. There is a possibility that an app you have
> developed for a site might turn out to have generic use. So, I think
> it is a good idea to always keep an app independent on the site as
> much as possible.
>
> Here is the question. Is it possible that to store the site name into
> a variable, such as site_name, then construct a statement like the
> following?
>
> statement = 'from ' + site_name + '.books.models import Publisher'
> exec(statement) # I am not sure if exec is a legitimate function. Here
> is just an example
Try removing mysite ...

from books.models import Publisher etc

If that stops it working you need to look at your Python path. Make sure
/path/to/mysite is on the pythonpath. Also check that you have
__init__.py in each of the sub-directories.

hth

Mike

>
>
>
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/Jn_07ca2t6MJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Praveen Krishna R

unread,
Aug 24, 2011, 2:15:56 AM8/24/11
to django...@googlegroups.com
Even if you write

from models import Publisher, Book, Author

its gonna work, inside the books application;

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Jn_07ca2t6MJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Thanks and Regards,
Praveen Krishna R

Gelonida N

unread,
Aug 24, 2011, 5:07:00 AM8/24/11
to django...@googlegroups.com
On 08/24/2011 08:15 AM, Praveen Krishna R wrote:
> *Even if you write

>
> from models import Publisher, Book, Author
>
> its gonna work, inside the books application;
> *

> On Wed, Aug 24, 2011 at 7:09 AM, Jim <jianb...@gmail.com
> <mailto:jianb...@gmail.com>> wrote:
>
It will be working, but I personally don't like relative imports too much.

If you really wanted to do relative imports, then I would use (for newer
python versions (>= 2.5)

# must be at the beginning of your python file
# the define will avoid any accidental relative imports
from __future__ import absolute_import

afterwards you can 'explicitely perform a relative import with'
from .models import Publisher, Book, Author # note the '.' before models

Reply all
Reply to author
Forward
0 new messages